-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathLogrotateCheck.py
More file actions
64 lines (56 loc) · 2.53 KB
/
Copy pathLogrotateCheck.py
File metadata and controls
64 lines (56 loc) · 2.53 KB
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
import os
from rpmlint.checks.AbstractCheck import AbstractCheck
class LogrotateCheck(AbstractCheck):
def check(self, pkg):
if pkg.is_source:
return
files = pkg.files
dirs = {}
for f in files:
if f in pkg.ghost_files:
continue
if f.startswith('/etc/logrotate.d/') or f.startswith('/usr/etc/logrotate.d/'):
try:
for n, o in self.parselogrotateconf(pkg.dir_name(), f).items():
if n in dirs and dirs[n] != o:
self.output.add_info('E', pkg, 'logrotate-duplicate', n)
else:
dirs[n] = o
except Exception as e:
self.output.add_info('E', pkg, 'logrotate-exception', f, str(e))
for d in sorted(dirs.keys()):
if d not in files:
self.output.add_info('E', pkg, 'logrotate-log-dir-not-packaged', d)
continue
mode = files[d].mode & 0o777
if ((files[d].user != 'root' and (dirs[d] is None or dirs[d][0] != files[d].user)) or
(files[d].group != 'root' and mode & 0o20 and (dirs[d] is None or dirs[d][1] != files[d].group))):
self.output.add_info('E', pkg, 'logrotate-user-writable-log-dir',
f'{d} {files[d].user}:{files[d].group} {mode:04o}')
# extremely primitive logrotate parser
def parselogrotateconf(self, root, f):
dirs = {}
with open('/'.join((root, f))) as fd:
currentdirs = []
for line in fd.readlines():
line = line.strip()
if line.startswith('#'):
continue
if not currentdirs:
if line.endswith('{'):
for logfile in line.split(' '):
logfile = logfile.strip()
if not logfile or logfile == '{':
continue
dn = os.path.dirname(logfile)
if dn not in dirs:
currentdirs.append(dn)
dirs[dn] = None
else:
if line.endswith('}'):
currentdirs = []
elif line.startswith('su '):
a = line.split(' ')
for dn in currentdirs:
dirs[dn] = (a[1], a[2])
return dirs