Prva inacica paketa za Debian jessie distribuciju.
[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         cp_echo "CN: Enabling $PKG configuration for Apache2."
47         return 0
48     }
49 else
50     cp_echo "CN: Could not load Apache 2.4 maintainer script helper."
51
52     modsecurity_enable() {
53         return 1
54     }
55 fi
56
57 # cleanup()
58 #
59 #   Cleanup all temp files or directories.
60 #
61 cleanup () {
62     local item
63
64     if [ -n "$temp_files" ]; then
65         for item in $temp_files; do
66             if [ -e "$item" ]; then
67                 rm -rf $item
68             fi
69         done
70     fi
71 }
72
73 # chk_conf_tag ()
74 #
75 #   Check if configuration file has CARNet package info lines.
76 #   return:  $RET => 0 - tagged
77 #                    1 - file does not exists
78 #                    2 - file exists, but it is not tagged
79 #
80 chk_conf_tag () {
81     local conf_file
82     conf_file="$1"
83     RET=1
84
85     if [ -f "$conf_file" ]; then
86         if egrep -q "^## Begin - Generated by CARNet package mod-security-cn$" "$conf_file"; then
87             RET=0
88         else
89             RET=2
90         fi
91     fi
92 }
93
94
95 # Set trap for deleting all temp files.
96 #
97 trap cleanup 0 1 2 15
98
99
100 # Remove obsolete symbolic link.
101 #
102 if [ "`readlink -q -m /etc/apache2/conf.d/$PKG.conf`" = "$MODSECCONF" ]; then
103     rm -f /etc/apache2/conf.d/$PKG.conf
104 fi
105
106
107 # Generate ModSecurity configuration files and activate RBL lookup
108 # for ModSecurity if needed.
109 #
110 chk_conf_tag "$MODSECCONF"
111 if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
112
113     # Create /etc/apache2/conf-available/ directory if missing.
114     if [ ! -d "$CONFDIR" ]; then
115         cp_echo "CN: Creating configuration directory $CONFDIR/"
116         mkdir -p $CONFDIR/
117     fi
118
119     # Create /etc/apache2/mod-security/ directory if missing.
120     if [ ! -d "$MODSECDIR" ]; then
121         cp_echo "CN: Creating ModSecurity configuration directory $MODSECDIR/"
122         mkdir -p $MODSECDIR/
123     fi
124
125     out=$(mktemp $MODSECCONF.XXXXXX)
126     temp_files="${temp_files} ${out}"
127
128     db_get mod-security-cn/rbl || true
129     if [ "$RET" = "true" ]; then
130
131         # Add RBL configuration.
132         chk_conf_tag "$MODSECRBL"
133         if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
134
135             if [ $RET -eq 1 ]; then
136                 cp_echo "CN: Creating configuration file $MODSECRBL"
137                 cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
138             else
139                 if ! cmp -s "$MODSECRBL" "$MODSECTPL/$(basename $MODSECRBL)"; then
140                     cp_echo "CN: Updating configuration file $MODSECRBL"
141                     cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
142                 fi
143             fi
144         fi
145
146         sed "s,#RBLLOOKUP#,Include $MODSECRBL,g" \
147             "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
148
149         if [ -e "$MODSECCONF" ]; then
150             if ! cmp -s "$MODSECCONF" "$out"; then
151                 cp_echo "CN: Updating configuration file $MODSECCONF"
152                 mv -f "$out" "$MODSECCONF"
153                 cp_echo "CN: Enabled ModSecurity RBL lookup."
154             fi
155         else
156             cp_echo "CN: Creating configuration file $MODSECCONF"
157             mv "$out" "$MODSECCONF"
158             cp_echo "CN: Enabled ModSecurity RBL lookup."
159         fi
160     else
161
162         # Remove RBL configuration.
163         sed "s,#RBLLOOKUP#,# DISABLED,g" \
164             "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
165
166         if [ -e "$MODSECCONF" ]; then
167             if ! cmp -s "$MODSECCONF" "$out"; then
168                 cp_echo "CN: Updating configuration file $MODSECCONF"
169                 mv -f "$out" "$MODSECCONF"
170                 cp_echo "CN: Disabled ModSecurity RBL lookup."
171             fi
172         else
173             cp_echo "CN: Creating configuration file $MODSECCONF"
174             mv "$out" "$MODSECCONF"
175             cp_echo "CN: Disabled ModSecurity RBL lookup."
176         fi
177
178         chk_conf_tag "$MODSECRBL"
179         if [ $RET -eq 0 ]; then
180             cp_echo "CN: Removing configuration file $MODSECRBL"
181             rm -f "$MODSECRBL"
182         fi
183     fi
184
185     if [ -f "$out" ]; then rm -f $out; fi
186
187     # Enable ModSecurity configuration.
188     if [ ! -e "$MODSECLNK" ]; then
189         ln -fs "$MODSECCONF" "$MODSECLNK"
190     fi
191     if modsecurity_enable; then
192         apache2_invoke enmod security2
193         apache2_invoke enconf security2-cn
194     fi
195 fi
196
197 db_stop || true
198
199 if ! apache2ctl configtest >/dev/null 2>&1; then
200     cp_echo "CN: Your Apache2 configuration seems to be broken."
201     cp_echo "CN: Please, check the service after the installation finishes!"
202 fi
203
204
205 # Mail root
206 #
207 cp_mail "$PKG"
208
209 #DEBHELPER#
210
211 exit 0