Ispravljena greska u debian/prerm. Modificiran 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.d"
33 A2MODEDIR="$A2DIR/mods-enabled"
34 MODSECDIR="$A2DIR/mod-security"
35 MODSECCONF="$MODSECDIR/mod-security-cn.conf"
36 MODSECRBL="$MODSECDIR/rbl_lookup.conf"
37 MODSECLNK="$CONFDIR/$(basename $MODSECCONF)"
38 MODSECTPL="/usr/share/mod-security-cn"
39
40 temp_files=
41 need_restart=0
42
43
44 # cleanup()
45 #
46 #   Cleanup all temp files or directories.
47 #
48 cleanup () {
49
50         local item
51
52         if [ -n "$temp_files" ]; then
53             for item in $temp_files; do
54                 if [ -e "$item" ]; then
55                     rm -rf $item
56                 fi
57             done
58         fi
59 }
60
61 # chk_conf_tag ()
62 #
63 #   Check if configuration file has CARNet package info lines.
64 #   return:  $RET => 0 - tagged
65 #                    1 - file does not exists
66 #                    2 - file exists, but it is not tagged
67 #
68 chk_conf_tag () {
69
70         local conf_file
71         conf_file="$1"
72         RET=1
73         
74         if [ -f "$conf_file" ]; then
75             if egrep -q "^## Begin - Generated by CARNet package mod-security-cn$" "$conf_file"; then
76                 RET=0
77             else
78                 RET=2
79             fi
80         fi
81 }
82
83
84 # Set trap for deleting all temp files.
85 #
86 trap cleanup 0 1 2 15
87
88
89 # Enable ModSecurity and unique_id Apache2 modules.
90 #
91 if [ -e "$CONF" ]; then
92
93         # Enable mod-security.load
94         if [ ! -e "$A2MODEDIR/mod-security.load" ]; then
95             cp_echo "CN: Enabling ModSecurity module for Apache2 web server."
96             a2enmod mod-security >/dev/null || true
97             need_restart=1
98         fi
99
100         # Enable unique_id.load
101         if [ ! -e "$A2MODEDIR/unique_id.load" ]; then
102             cp_echo "CN: Enabling unique_id module for Apache2 web server."
103             a2enmod unique_id >/dev/null || true
104             need_restart=1
105         fi
106 fi
107
108
109 # Generate ModSecurity configuration files and activate RBL lookup
110 # for ModSecurity if needed.
111 #
112 chk_conf_tag "$MODSECCONF"
113 if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
114
115         # Create /etc/apache2/conf.d/ directory if missing.
116         if [ ! -d "$CONFDIR" ]; then
117             cp_echo "CN: Creating configuration directory $CONFDIR/"
118             mkdir -p $CONFDIR/
119         fi
120
121         # Create /etc/apache2/mod-security/ directory if missing.
122         if [ ! -d "$MODSECDIR" ]; then
123             cp_echo "CN: Creating ModSecurity configuration directory $MODSECDIR/"
124             mkdir -p $MODSECDIR/
125         fi
126
127         out=$(mktemp $MODSECCONF.XXXXXX)
128         temp_files="${temp_files} ${out}"
129
130         db_get mod-security-cn/rbl || true
131         if [ "$RET" = "true" ]; then
132
133             # Add RBL configuration.
134             chk_conf_tag "$MODSECRBL"
135             if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
136
137                 if [ $RET -eq 1 ]; then
138                     cp_echo "CN: Creating configuration file $MODSECRBL"
139                     cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
140                     need_restart=1
141                 else
142                     if ! cmp -s "$MODSECRBL" "$MODSECTPL/$(basename $MODSECRBL)"; then
143                         cp_echo "CN: Updating configuration file $MODSECRBL"
144                         cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
145                         need_restart=1
146                     fi
147                 fi
148             fi
149
150             sed "s,#RBLLOOKUP#,Include $MODSECRBL,g" \
151                 "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
152
153             if [ -e "$MODSECCONF" ]; then
154                 if ! cmp -s "$MODSECCONF" "$out"; then
155                     cp_echo "CN: Updating configuration file $MODSECCONF"
156                     mv -f "$out" "$MODSECCONF"
157                     cp_echo "CN: Enabled ModSecurity RBL lookup."
158                     need_restart=1
159                 fi
160             else
161                 cp_echo "CN: Creating configuration file $MODSECCONF"
162                 mv "$out" "$MODSECCONF"
163                 cp_echo "CN: Enabled ModSecurity RBL lookup."
164                 need_restart=1
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                     need_restart=1
178                 fi
179             else
180                 cp_echo "CN: Creating configuration file $MODSECCONF"
181                 mv "$out" "$MODSECCONF"
182                 cp_echo "CN: Disabled ModSecurity RBL lookup."
183                 need_restart=1
184             fi
185
186             chk_conf_tag "$MODSECRBL"
187             if [ $RET -eq 0 ]; then
188                 cp_echo "CN: Removing configuration file $MODSECRBL"
189                 rm -f "$MODSECRBL"
190                 need_restart=1
191             fi
192         fi
193
194         if [ -f "$out" ]; then rm -f $out; fi
195
196         # Enable ModSecurity configuration.
197         if [ ! -e "$MODSECLNK" ]; then
198             cp_echo "CN: Enabling ModSecurity configuration."
199             ln -fs "$MODSECCONF" "$MODSECLNK"
200             need_restart=1
201         fi
202 fi
203
204 db_stop || true
205
206
207 # Restart Apache2 web server if needed.
208 #
209 if [ $need_restart -eq 1 ]; then
210
211         # Check Apache2 web server configuration.
212         if /usr/sbin/apache2ctl configtest 2>/dev/null; then
213
214             # Restart Apache2 web server.
215             if [ -x "/etc/init.d/apache2" ]; then
216                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
217                     invoke-rc.d apache2 restart || true
218                 else
219                     /etc/init.d/apache2 restart || true
220                 fi
221             fi
222         else
223
224             # Something is broken.
225             cp_echo "CN: Your Apache2 configuration is broken."
226             cp_echo "CN: Please, check the service after the installation finishes!"
227         fi
228 fi
229
230
231 # Mail root
232 #
233 cp_mail "$PKG"
234
235 exit 0