5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
12 abort-upgrade|abort-remove|abort-deconfigure)
17 echo "postinst called with unknown argument \`$1'" >&2
24 . /usr/share/debconf/confmodule
26 # Include CARNet functions
27 . /usr/share/carnet-tools/functions.sh
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"
46 # Cleanup all temp files or directories.
51 if [ -n "$temp_files" ]; then
52 for item in $temp_files; do
53 if [ -e "$item" ]; then
62 # Check if configuration file has CARNet package info lines.
63 # return: $RET => 0 - tagged
64 # 1 - file does not exists
65 # 2 - file exists, but it is not tagged
72 if [ -f "$conf_file" ]; then
73 if egrep -q "^## Begin - Generated by CARNet package mod-security-cn$" "$conf_file"; then
82 # Set trap for deleting all temp files.
87 # Enable ModSecurity and unique_id Apache2 modules.
89 if [ -e "$CONF" ]; then
90 # Enable mod-security.load
91 if [ ! -e "$A2MODEDIR/mod-security.load" ]; then
92 cp_echo "CN: Enabling ModSecurity module for Apache2 web server."
93 a2enmod mod-security >/dev/null || true
97 # Enable unique_id.load
98 if [ ! -e "$A2MODEDIR/unique_id.load" ]; then
99 cp_echo "CN: Enabling unique_id module for Apache2 web server."
100 a2enmod unique_id >/dev/null || true
106 # Generate ModSecurity configuration files and activate RBL lookup
107 # for ModSecurity if needed.
109 chk_conf_tag "$MODSECCONF"
110 if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
112 # Create /etc/apache2/conf.d/ directory if missing.
113 if [ ! -d "$CONFDIR" ]; then
114 cp_echo "CN: Creating configuration directory $CONFDIR/"
118 # Create /etc/apache2/mod-security/ directory if missing.
119 if [ ! -d "$MODSECDIR" ]; then
120 cp_echo "CN: Creating ModSecurity configuration directory $MODSECDIR/"
124 out=$(mktemp $MODSECCONF.XXXXXX)
125 temp_files="${temp_files} ${out}"
127 db_get mod-security-cn/rbl || true
128 if [ "$RET" = "true" ]; then
130 # Add RBL configuration.
131 chk_conf_tag "$MODSECRBL"
132 if [ $RET -eq 0 ] || [ $RET -eq 1 ]; then
134 if [ $RET -eq 1 ]; then
135 cp_echo "CN: Creating configuration file $MODSECRBL"
136 cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
139 if ! cmp -s "$MODSECRBL" "$MODSECTPL/$(basename $MODSECRBL)"; then
140 cp_echo "CN: Updating configuration file $MODSECRBL"
141 cp "$MODSECTPL/$(basename $MODSECRBL)" "$MODSECRBL"
147 sed "s,#RBLLOOKUP#,Include $MODSECRBL,g" \
148 "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
150 if [ -e "$MODSECCONF" ]; then
151 if ! cmp -s "$MODSECCONF" "$out"; then
152 cp_echo "CN: Updating configuration file $MODSECCONF"
153 mv -f "$out" "$MODSECCONF"
154 cp_echo "CN: Enabled ModSecurity RBL lookup."
158 cp_echo "CN: Creating configuration file $MODSECCONF"
159 mv "$out" "$MODSECCONF"
160 cp_echo "CN: Enabled ModSecurity RBL lookup."
165 # Remove RBL configuration.
166 sed "s,#RBLLOOKUP#,# DISABLED,g" \
167 "$MODSECTPL/$(basename $MODSECCONF)" > "$out"
169 if [ -e "$MODSECCONF" ]; then
170 if ! cmp -s "$MODSECCONF" "$out"; then
171 cp_echo "CN: Updating configuration file $MODSECCONF"
172 mv -f "$out" "$MODSECCONF"
173 cp_echo "CN: Disabled ModSecurity RBL lookup."
177 cp_echo "CN: Creating configuration file $MODSECCONF"
178 mv "$out" "$MODSECCONF"
179 cp_echo "CN: Disabled ModSecurity RBL lookup."
183 chk_conf_tag "$MODSECRBL"
184 if [ $RET -eq 0 ]; then
185 cp_echo "CN: Removing configuration file $MODSECRBL"
191 if [ -f "$out" ]; then rm -f $out; fi
193 # Enable ModSecurity configuration.
194 if [ ! -e "$MODSECLNK" ]; then
195 cp_echo "CN: Enabling ModSecurity configuration."
196 ln -fs "$MODSECCONF" "$MODSECLNK"
204 # Restart Apache2 web server if needed.
206 if [ $need_restart -eq 1 ]; then
208 # Check Apache2 web server configuration.
209 if apache2ctl configtest 2>/dev/null; then
210 invoke-rc.d apache2 force-reload || true
212 # Something is broken.
213 cp_echo "CN: Your Apache2 configuration is broken."
214 cp_echo "CN: Please, check the service after the installation finishes!"