Passed
Push — master ( dbd4af...e9c47c )
by Jan
02:33 queued 11s
created

template._file_owner_groupowner_permissions_regex()   A

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
def _file_owner_groupowner_permissions_regex(data):
2
    data["is_directory"] = data["filepath"].endswith("/")
3
    if "missing_file_pass" not in data:
4
        data["missing_file_pass"] = False
5
    if "file_regex" in data and not data["is_directory"]:
6
        raise ValueError(
7
            "Used 'file_regex' key in rule '{0}' but filepath '{1}' does not "
8
            "specify a directory. Append '/' to the filepath or remove the "
9
            "'file_regex' key.".format(data["_rule_id"], data["filepath"]))
10
11
12
def preprocess(data, lang):
13
    _file_owner_groupowner_permissions_regex(data)
14
    if lang == "oval":
15
        data["fileid"] = data["_rule_id"].replace("file_permissions", "")
16
        # build the state that describes our mode
17
        # mode_str maps to STATEMODE in the template
18
        mode = data["filemode"]
19
        fields = [
20
            'oexec', 'owrite', 'oread', 'gexec', 'gwrite', 'gread',
21
            'uexec', 'uwrite', 'uread', 'sticky', 'sgid', 'suid']
22
        mode_int = int(mode, 8)
23
        mode_str = ""
24
        for field in fields:
25
            if mode_int & 0x01 == 1:
26
                mode_str = (
27
                    "	<unix:" + field + " datatype=\"boolean\">true</unix:"
28
                    + field + ">\n" + mode_str)
29
            else:
30
                mode_str = (
31
                    "	<unix:" + field + " datatype=\"boolean\">false</unix:"
32
                    + field + ">\n" + mode_str)
33
            mode_int = mode_int >> 1
34
        data["statemode"] = mode_str
35
    return data
36