Imported Upstream version 2.4.3
[platform/upstream/audit.git] / init.d / augenrules
1 #!/bin/bash
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 usage="Usage: $0 [--check|--load]"
36
37 # Delete the interim file on faults
38 trap 'rm -f ${TmpRules}; exit 1' 1 2 3 13 15
39
40 try_load() {
41         if [ $LoadRules -eq 1 ] ; then
42                 auditctl -R ${DestinationFile}
43                 RETVAL=$?
44         fi
45 }
46
47 while [ $# -ge 1 ]
48 do
49         if [ "$1" = "--check" ] ; then
50                 OnlyCheck=1
51         elif [ "$1" = "--load" ] ; then
52                 LoadRules=1
53         else
54                 echo "$usage"
55                 exit 1
56         fi
57         shift
58 done
59
60 # Check environment
61 if [ ! -d ${SourceRulesDir} ]; then
62         echo "$0: No rules directory - ${SourceRulesDir}"
63         rm -f ${TmpRules}
64         try_load
65         exit 1
66 fi
67
68 # Create the interim rules file ensuring its access modes protect it
69 # from normal users and strip empty lines and comment lines. We also ensure
70 #   - the last processed -D directive without an option is emitted as the first
71 #     line. -D directives with options are left in place
72 #   - the last processed -b directory is emitted as the second line
73 #   - the last processed -f directory is emitted as the third line
74 #   - the last processed -e directive is emitted as the last line
75 umask 0137
76 echo "## This file is automatically generated from $SourceRulesDir" >> ${TmpRules}
77 for rules in $(/bin/ls -1v ${SourceRulesDir} | grep ".rules$") ; do
78         cat ${SourceRulesDir}/${rules}
79 done | awk '\
80 BEGIN   {
81         minus_e = "";
82         minus_D = "";
83         minus_f = "";
84         minus_b = "";
85         rest = 0;
86 } {
87         if (length($0) < 1) { next; }
88         if (match($0, "^\\s*#")) { next; }
89         if (match($0, "^\\s*-e")) { minus_e = $0; next; }
90         if (match($0, "^\\s*-D\\s*$")) { minus_D = $0; next; }
91         if (match($0, "^\\s*-f")) { minus_f = $0; next; }
92         if (match($0, "^\\s*-b")) { minus_b = $0; next; }
93         rules[rest++] = $0;
94 }
95 END     {
96         printf "%s\n%s\n%s\n", minus_D, minus_b, minus_f;
97         for (i = 0; i < rest; i++) { printf "%s\n", rules[i]; }
98         printf "%s\n", minus_e;
99 }' >> ${TmpRules}
100
101 # If empty then quit
102 if [ ! -s ${TmpRules} ]; then
103         echo "$0: No rules"
104         rm -f ${TmpRules}
105         try_load
106         exit $RETVAL
107 fi
108
109 # If the same then quit
110 cmp -s ${TmpRules} ${DestinationFile} > /dev/null 2>&1
111 if [ $? -eq 0 ]; then
112         echo "$0: No change"
113         rm -f ${TmpRules}
114         try_load
115         exit $RETVAL
116 elif [ $OnlyCheck -eq 1 ] ; then
117         echo "$0: Rules have changed and should be updated"
118         exit 0
119 fi
120
121 # Otherwise we install the new file
122 if [ -f ${DestinationFile} ]; then
123         cp ${DestinationFile} ${DestinationFile}.prev
124 fi
125 # We copy the file so that it gets the right selinux lable
126 cp ${TmpRules} ${DestinationFile}
127 rm -f ${TmpRules}
128
129 try_load
130 exit $RETVAL