Debian Codegraph @

init.d/augenrules

raw
1 #!/bin/sh
2
3 # Script to concatenate rules files found in a base audit rules directory
4 # to form a single /etc/audit/audit.rules file suitable for loading into
5 # the Linux audit system
6
7 # When forming the interim rules file, both empty lines and comment
8 # lines (starting with # or <whitespace>#) are stripped as the source files
9 # are processed.
10 #
11 # Having formed the interim rules file, the script checks if the file is empty
12 # or is identical to the existing /etc/audit/audit.rules and if either of
13 # these cases are true, it does not replace the existing file
14 #
15
16 # Variables
17 #
18 # DestinationFile:
19 # Destination rules file
20 # SourceRulesDir:
21 # Directory location to find component rule files
22 # TmpRules:
23 # Temporary interim rules file
24 # ASuffix:
25 # Suffix for previous audit.rules file if this script replaces it.
26 # The file is left in the destination directory with suffix with $ASuffix
27
28 DestinationFile=/etc/audit/audit.rules
29 SourceRulesDir=/etc/audit/rules.d
30 TmpRules=$(mktemp /tmp/aurules.XXXXXXXX)
31 ASuffix="prev"
32 OnlyCheck=0
33 LoadRules=0
34 RETVAL=0
35 cmd="$0"
36 usage="Usage: $cmd [--check|--load]"
37
38 # Delete the interim file on faults
39 trap 'rm -f ${TmpRules}; exit 1' HUP INT QUIT PIPE TERM
40
41 try_load() {
42 if [ $LoadRules -eq 1 ] ; then
43 /sbin/auditctl -R ${DestinationFile}
44 RETVAL=$?
45 fi
46 }
47
48 # Check if audit is in immutable mode - exit if so
49 check_immutable () {
50 if [ "$(auditctl -s | awk '$1 == "enabled" { print $2 }')" = "2" ] ; then
51 echo "$cmd: Audit system is in immutable mode - exiting with no changes"
52 exit 0
53 fi
54 }
55
56 while [ $# -ge 1 ]
57 do
58 if [ "$1" = "--check" ] ; then
59 OnlyCheck=1
60 elif [ "$1" = "--load" ] ; then
61 LoadRules=1
62 else
63 echo "$usage"
64 exit 1
65 fi
66 shift
67 done
68
69 # Check environment
70 if [ ! -d ${SourceRulesDir} ]; then
71 echo "$cmd: No rules directory - ${SourceRulesDir}"
72 rm -f "${TmpRules}"
73 try_load
74 exit 1
75 fi
76
77 # Create the interim rules file ensuring its access modes protect it
78 # from normal users and strip empty lines and comment lines. We also ensure
79 # - the last processed -D directive without an option is emitted as the first
80 # line. -D directives with options are left in place
81 # - the last processed -b directory is emitted as the second line
82 # - the last processed -f directory is emitted as the third line
83 # - the last processed -e directive is emitted as the last line
84 umask 0137
85 echo "## This file is automatically generated from $SourceRulesDir" >> "${TmpRules}"
86 for rules in $(/bin/ls -1v ${SourceRulesDir} | grep "\.rules$") ; do
87 cat ${SourceRulesDir}/"${rules}"
88 done | awk '
89 BEGIN {
90 minus_e = "";
91 minus_D = "";
92 minus_f = "";
93 minus_b = "";
94 rest = 0;
95 } {
96 sub(/\r$/, "");
97 if (length($0) < 1) { next; }
98 if (match($0, "^\\s*#")) { next; }
99 if (match($0, "^\\s*-e")) { minus_e = $0; next; }
100 if (match($0, "^\\s*-D\\s*$")) { minus_D = $0; next; }
101 if (match($0, "^\\s*-f")) { minus_f = $0; next; }
102 if (match($0, "^\\s*-b")) { minus_b = $0; next; }
103 rules[rest++] = $0;
104 }
105 END {
106 printf "%s\n%s\n%s\n", minus_D, minus_b, minus_f;
107 for (i = 0; i < rest; i++) { printf "%s\n", rules[i]; }
108 printf "%s\n", minus_e;
109 }' >> "${TmpRules}"
110
111 # If empty then quit
112 if [ ! -s "${TmpRules}" ]; then
113 echo "$cmd: No rules"
114 rm -f "${TmpRules}"
115 try_load
116 exit $RETVAL
117 fi
118
119 # If the same then quit
120 cmp -s "${TmpRules}" ${DestinationFile} > /dev/null 2>&1
121 if [ $? -eq 0 ]; then
122 echo "$cmd: No change"
123 rm -f "${TmpRules}"
124 check_immutable
125 try_load
126 exit $RETVAL
127 elif [ $OnlyCheck -eq 1 ] ; then
128 echo "$cmd: Rules have changed and should be updated"
129 rm -f "${TmpRules}"
130 exit 0
131 fi
132
133 # Otherwise we install the new file
134 check_immutable
135 if [ -f ${DestinationFile} ]; then
136 cp ${DestinationFile} ${DestinationFile}.${ASuffix}
137 fi
138 # We copy the file so that it gets the right selinux label
139 cp "${TmpRules}" ${DestinationFile}
140 chmod 0640 ${DestinationFile}
141 # Restore context on MLS system. /tmp is SystemLow & audit.rules is SystemHigh
142 if [ -x /usr/sbin/restorecon ] ; then
143 /usr/sbin/restorecon -F ${DestinationFile}
144 fi
145 rm -f "${TmpRules}"
146
147 try_load
148 exit $RETVAL
149