-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathchecks_linux.go
264 lines (237 loc) Β· 6.1 KB
/
checks_linux.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"errors"
"os"
"os/user"
"strconv"
"strings"
"syscall"
)
func (c cond) AutoCheckUpdatesEnabled() (bool, error) {
result, err := cond{
Path: "/etc/apt/apt.conf.d/",
Value: `(?i)^\s*APT::Periodic::Update-Package-Lists\s+"1"\s*;\s*$`,
regex: true,
}.DirContains()
// If /etc/apt/ does not exist, try dnf (RHEL)
if err != nil {
autoConf, err := cond{
Path: "/etc/dnf/automatic.conf",
}.PathExists()
if err != nil {
return false, err
}
if autoConf {
applyUpdates, err := cond{
Path: "/etc/dnf/automatic.conf",
Value: `(?i)^\s*apply_updates\s*=\s*(1|on|yes|true)`,
regex: true,
}.FileContains()
if err != nil {
return false, err
}
autoTimer, err := cond{
Path: "/etc/systemd/system/timers.target.wants/dnf-automatic.timer",
}.PathExists()
if err != nil {
return false, err
}
if applyUpdates && autoTimer {
return true, nil
}
autoInstallTimer, err := cond{
Path: "/etc/systemd/system/timers.target.wants/dnf-automatic-install.timer",
}.PathExists()
if err != nil {
return false, err
}
return autoInstallTimer, nil
}
}
return result, err
}
// Command checks if a given shell command ran successfully (that is, did not
// return or raise any errors).
func (c cond) Command() (bool, error) {
c.requireArgs("Cmd")
if c.Cmd == "" {
fail("Missing command for", c.Type)
}
err := shellCommand(c.Cmd)
if err != nil {
// This check does not return errors, since it is based on successful
// execution. If any errors occurred, it means that the check failed,
// not errored out.
//
// It would be an error if failure to execute the command resulted in
// an inability to meaningfully score the check (e.g., if the uname
// syscall failed for KernelVersion).
return false, nil
}
return true, nil
}
func (c cond) FirewallUp() (bool, error) {
result, err := cond{
Path: "/etc/ufw/ufw.conf",
Value: `^\s*ENABLED=yes\s*$`,
regex: true,
}.FileContains()
if err != nil {
// If ufw.conf does not exist, check firewalld status (RHEL)
return cond{
Name: "firewalld",
}.ServiceUp()
}
return result, err
}
func (c cond) GuestDisabledLDM() (bool, error) {
guestStr := `\s*allow-guest\s*=\s*false`
result, err := cond{
Path: "/usr/share/lightdm/lightdm.conf.d/",
Value: guestStr,
regex: true,
}.DirContains()
if !result {
return cond{
Path: "/etc/lightdm/",
Value: guestStr,
regex: true,
}.DirContains()
}
return result, err
}
func (c cond) KernelVersion() (bool, error) {
c.requireArgs("Value")
utsname := syscall.Utsname{}
err := syscall.Uname(&utsname)
releaseUint := []byte{}
for i := 0; i < 65; i++ {
if utsname.Release[i] == 0 {
break
}
releaseUint = append(releaseUint, uint8(utsname.Release[i]))
}
debug("System uname value is", string(releaseUint), "and our value is", c.Value)
return string(releaseUint) == c.Value, err
}
func (c cond) PasswordChanged() (bool, error) {
c.requireArgs("User", "Value")
fileContent, err := readFile("/etc/shadow")
if err != nil {
return false, err
}
for _, line := range strings.Split(fileContent, "\n") {
if strings.Contains(line, c.User+":") {
if strings.Contains(line, c.User+":"+c.Value) {
debug("Exact value found in /etc/shadow for user", c.User+":", line)
return false, nil
}
debug("Differing value found in /etc/shadow for user", c.User+":", line)
return true, nil
}
}
return false, errors.New("user not found")
}
func (c cond) FileOwner() (bool, error) {
c.requireArgs("Path", "Name")
u, err := user.Lookup(c.Name)
if err != nil {
return false, err
}
f, err := os.Stat(c.Path)
if err != nil {
return false, err
}
uid := f.Sys().(*syscall.Stat_t).Uid
o, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return false, err
}
debug("File owner for", c.Path, "uid is", strconv.FormatUint(uint64(uid), 10))
return uint32(o) == uid, nil
}
func (c cond) PermissionIs() (bool, error) {
c.requireArgs("Path", "Value")
f, err := os.Stat(c.Path)
if err != nil {
return false, err
}
fileMode := f.Mode()
modeBytes := []byte(fileMode.String())
if len(modeBytes) != 10 {
fail("System permission string is wrong length:", string(modeBytes))
return false, errors.New("Invalid system permission string")
}
// Permission string includes suid/sgid as the special bit (MSB), while
// GNU coreutils replaces the executable bit, which we need to emulate.
if fileMode&os.ModeSetuid != 0 {
modeBytes[0] = '-'
modeBytes[3] = 's'
}
if fileMode&os.ModeSetgid != 0 {
modeBytes[0] = '-'
modeBytes[6] = 's'
}
c.Value = strings.TrimSpace(c.Value)
if len(c.Value) == 9 {
// If we're provided a mode string of only 9 characters, we'll assume
// that the 0th bit is irrelevant and should be a wildcard
c.Value = "?" + c.Value
} else if len(c.Value) != 10 {
fail("Your permission string is the wrong length (should be 9 or 10 characters):", c.Value)
return false, errors.New("Invalid user permission string")
}
for i := 0; i < len(c.Value); i++ {
if c.Value[i] == '?' {
continue
}
if c.Value[i] != modeBytes[i] {
return false, nil
}
}
return true, nil
}
func (c cond) ProgramInstalled() (bool, error) {
c.requireArgs("Name")
result, err := cond{
Cmd: "dpkg -s " + c.Name,
Value: " install",
}.CommandContains()
// If dpkg fails, use rpm
if err != nil {
return cond{
Cmd: "rpm -q " + c.Name,
}.Command()
}
return result, err
}
func (c cond) ProgramVersion() (bool, error) {
c.requireArgs("Name", "Value")
return cond{
Cmd: `dpkg -s ` + c.Name + ` | grep Version | cut -d" " -f2`,
Value: c.Value,
}.CommandOutput()
}
func (c cond) ServiceUp() (bool, error) {
// TODO: detect and use other init systems
c.requireArgs("Name")
return cond{
Cmd: "systemctl is-active " + c.Name,
}.Command()
}
func (c cond) UserExists() (bool, error) {
c.requireArgs("User")
return cond{
Path: "/etc/passwd",
Value: "^" + c.User + ":",
regex: true,
}.FileContains()
}
func (c cond) UserInGroup() (bool, error) {
c.requireArgs("User", "Group")
return cond{
Path: "/etc/group",
Value: c.Group + `[0-9a-zA-Z,:\s+]+` + c.User,
regex: true,
}.FileContains()
}