Nacin aktivacije ModSecurity u debian/postinst.
[mod-security-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
6
7 case "$1" in
8     configure)
9     # continue below
10     ;;
11
12     abort-upgrade|abort-remove|abort-deconfigure)
13     exit 0
14     ;;
15
16     *)
17     echo "postinst called with unknown argument \`$1'" >&2
18     exit 0
19     ;;
20 esac
21
22
23 # Load debconf
24 . /usr/share/debconf/confmodule
25
26 # Include CARNet functions
27 . /usr/share/carnet-tools/functions.sh
28
29 PKG="mod-security-cn"
30 A2DIR="/etc/apache2"
31 CONF="$A2DIR/apache2.conf"
32 CONFDIR="$A2DIR/conf-available"
33 MODSECDIR="$A2DIR/mod-security"
34 MODSECCONF="$MODSECDIR/mod-security-cn.conf"
35 MODSECRBL="$MODSECDIR/rbl_lookup.conf"
36 MODSECLNK="$CONFDIR/security2-cn.conf"
37 MODSECTPL="/usr/share/mod-security-cn"
38
39 temp_files=
40
41
42 if [ -e /usr/share/apache2/apache2-maintscript-helper ]; then
43     . /usr/share/apache2/apache2-maintscript-helper
44
45     modsecurity_enable() {
46         return 0
47     }
48 else
49     cp_echo "CN: Could not load Apache 2.4 maintainer script helper."
50
51     modsecurity_enable() {
52         return 1
53     }
54 fi
55
56 # cleanup()
57 #
58 #   Cleanup all temp files or directories.
59 #
60 cleanup () {
61     local item
62
63     if [ -n "$temp_files" ]; then
64         for item in $temp_files; do
65             if [ -e "$item" ]; then
66                 rm -rf $item
67             fi
68         done
69     fi
70 }
71
72 # chk_conf_tag ()
73 #
74 #   Check if configuration file has CARNet package info lines.
75 #   return:  $RET => 0 - tagged
76 #                    1 - file does not exists
77 #                    2 - file exists, but it is not tagged
78 #
79 chk_conf_tag () {
80     local conf_file
81     conf_file="$1"
82     RET=1
83
84     if [ -f "$conf_file" ]; then
85         if egrep -q "^## Begin - Generated by CARNet package mod-security-cn$" "$conf_file"; then
86             RET=0
87         else
88             RET=2
89         fi
90     fi
91 }
92
93
94 # Set trap for deleting all temp files.
95 #
96 trap cleanup 0 1 2 15
97
98
99 # Enable ModSecurity and unique_id Apache2 modules.
100 #
101 if modsecurity_enable; then
102     apache2_invoke enmod security2
103 fi
104
105
106 # Remove obsolete symbolic link.
107 #
108 if [ "`readlink -q -m /etc/apache2/conf.d/$PKG.conf`" = "$MODSECCONF" ]; then
109     rm -f /etc/apache2/conf.d/$PKG.conf
110 fi
111
112
113 # Generate ModSecurity configuration files and activate RBL lookup
114 # for ModSecurity if needed.
115 #
116 chk_conf_tag "$MODSECCONF"
117 if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
118
119     # Create /etc/apache2/conf-available/ directory if missing.
120     if [ ! -d "$CONFDIR" ]; then
121         cp_echo "CN: Creating configuration directory $CONFDIR/"
122         mkdir -p $CONFDIR/
123     fi
124
125     # Create /etc/apache2/mod-security/ directory if missing.
126     if [ ! -d "$MODSECDIR" ]; then
127         cp_echo "CN: Creating ModSecurity configuration directory $MODSECDIR/"
128         mkdir -p $MODSECDIR/
129     fi
130
131     out=$(mktemp $MODSECCONF.XXXXXX)
132     temp_files="${temp_files} ${out}"
133
134     db_get mod-security-cn/rbl || true
135     if [ "$RET" = "true" ]; then
136
137         # Add RBL configuration.
138         chk_conf_tag "$MODSECRBL"
139         if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
140
141             if [ $RET -eq 1 ]; then
142                 cp_echo "CN: Creating configuration file $MODSECRBL"
143                 cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
144             else
145                 if ! cmp -s "$MODSECRBL" "$MODSECTPL/$(basename $MODSECRBL)"; then
146                     cp_echo "CN: Updating configuration file $MODSECRBL"
147                     cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
148                 fi
149             fi
150         fi
151
152         sed "s,#RBLLOOKUP#,Include $MODSECRBL,g" \
153             "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
154
155         if [ -e "$MODSECCONF" ]; then
156             if ! cmp -s "$MODSECCONF" "$out"; then
157                 cp_echo "CN: Updating configuration file $MODSECCONF"
158                 mv -f "$out" "$MODSECCONF"
159                 cp_echo "CN: Enabled ModSecurity RBL lookup."
160             fi
161         else
162             cp_echo "CN: Creating configuration file $MODSECCONF"
163             mv "$out" "$MODSECCONF"
164             cp_echo "CN: Enabled ModSecurity RBL lookup."
165         fi
166     else
167
168         # Remove RBL configuration.
169         sed "s,#RBLLOOKUP#,# DISABLED,g" \
170             "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
171
172         if [ -e "$MODSECCONF" ]; then
173             if ! cmp -s "$MODSECCONF" "$out"; then
174                 cp_echo "CN: Updating configuration file $MODSECCONF"
175                 mv -f "$out" "$MODSECCONF"
176                 cp_echo "CN: Disabled ModSecurity RBL lookup."
177             fi
178         else
179             cp_echo "CN: Creating configuration file $MODSECCONF"
180             mv "$out" "$MODSECCONF"
181             cp_echo "CN: Disabled ModSecurity RBL lookup."
182         fi
183
184         chk_conf_tag "$MODSECRBL"
185         if [ $RET -eq 0 ]; then
186             cp_echo "CN: Removing configuration file $MODSECRBL"
187             rm -f "$MODSECRBL"
188         fi
189     fi
190
191     if [ -f "$out" ]; then rm -f $out; fi
192 fi
193
194
195 # Enable ModSecurity configuration.
196 #
197 if [ ! -e "$MODSECLNK" ]; then
198     ln -fs "$MODSECCONF" "$MODSECLNK"
199 fi
200 if modsecurity_enable; then
201     cp_echo "CN: Enabling $PKG configuration for Apache2."
202     apache2_invoke enconf security2-cn
203 fi
204
205 db_stop || true
206
207 if ! apache2ctl configtest >/dev/null 2>&1; then
208     cp_echo "CN: Your Apache2 configuration seems to be broken."
209     cp_echo "CN: Please, check the service after the installation finishes!"
210 fi
211
212
213 # Mail root
214 #
215 cp_mail "$PKG"
216
217 #DEBHELPER#
218
219 exit 0