+#!/bin/sh
+# postinst script for spamassassin-cn
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+# * <postinst> `configure' <most-recently-configured-version>
+# * <old-postinst> `abort-upgrade' <new version>
+# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
+# <new-version>
+# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
+# <failed-install-package> <version> `removing'
+# <conflicting-package> <version>
+# for details, see http://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+#
+
+case "$1" in
+ configure|reconfigure)
+ # continue below
+ ;;
+
+ *)
+ exit 0
+ ;;
+esac
+
+# import CN-functions
+. /usr/share/carnet-tools/functions.sh
+
+# load Debconf
+. /usr/share/debconf/confmodule
+db_version 2.0
+
+umask 022
+
+db_get spamassassin-cn/usebayes || true
+use_bayes="$RET"
+
+db_get spamassassin-cn/userazor2 || true
+use_razor2="$RET"
+
+db_get spamassassin-cn/usedcc || true
+use_dcc="$RET"
+
+db_get spamassassin-cn/runservice || true
+run_service="$RET"
+
+db_get spamassassin-cn/runasuser || true
+run_as_user="$RET"
+
+db_stop || true
+
+# run spamd service as non-root
+if [ "$run_as_user" = "non-root" ]; then
+ if ! grep -q '^OPTIONS=".*username=spamd"' \
+ /etc/default/spamassassin; then
+ if [ -x /usr/sbin/invoke-rc.d ]; then
+ invoke-rc.d --quiet spamassassin stop
+ else
+ /etc/init.d/spamassassin stop
+ fi
+ rstart=yes
+ fi
+
+ if ! getent group spamd >/dev/null 2>&1; then
+ echo "CN: Adding group spamd"
+ addgroup --system spamd >/dev/null 2>&1
+ fi
+ if ! getent passwd spamd >/dev/null 2>&1; then
+ echo "CN: Adding user spamd"
+ adduser --system --no-create-home --home /etc/spamassassin \
+ --gecos "SpamAssassin User" \
+ --ingroup spamd --disabled-password spamd >/dev/null 2>&1
+ fi
+
+ if [ ! -d "/var/run/spamd" ]; then
+ mkdir /var/run/spamd
+ fi
+
+ chown -R spamd:spamd /var/run/spamd/
+ dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
+ dpkg-statoverride --update --add spamd spamd 755 /var/run/spamd \
+ >/dev/null 2>&1 || true
+
+# nope, run spamd as root
+elif [ "$run_as_user" = "root" ]; then
+ if grep -q '^OPTIONS=".*username=spamd"' \
+ /etc/default/spamassassin; then
+ if [ -x /usr/sbin/invoke-rc.d ]; then
+ invoke-rc.d --quiet spamassassin stop
+ else
+ /etc/init.d/spamassassin stop
+ fi
+ restart=yes
+ fi
+
+ if [ -d "/var/run/spamd" ]; then
+ rm -rf /var/run/spamd
+ fi
+ dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
+fi
+
+examdir="/usr/share/doc/spamassassin-cn/examples"
+confdir="/etc/spamassassin"
+
+# check and replace local.cf if needed
+if [ -f $examdir/local.cf ]; then
+ # non-existant or zero
+ if [ ! -s $confdir/local.cf ]; then
+ rm -f $confdir/local.cf
+ fi
+
+ # old enough
+ if [ -s $confdir/local.cf ] &&
+ dpkg --compare-versions "$2" lt "2:3.0.3-3"; then
+ echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
+ cp_backup_conffile $confdir/local.cf
+ rm -f $confdir/local.cf
+ fi
+
+ # copy template
+ cp -f $examdir/local.cf $confdir/local.cf.$$
+
+ # merge both configurations
+ if [ -s $confdir/local.cf ]; then
+ #cat $confdir/local.cf > $confdir/local.cf.$$
+
+ # merge old and new in one conf, primarily respecting old
+ script='
+ my %confhash = ();
+ my ($key, $value);
+
+ # read template
+ open (NEW, "<", $ARGV[0]);
+ while (<NEW>)
+ {
+ chomp();
+ if (/^(\S+)\s+(.+)$/o)
+ {
+ my ($key, $value) = ($1, $2);
+ $hash{$key} = $value;
+ }
+ }
+ close(NEW);
+
+ # read old configuration
+ open (OLD, "<", $ARGV[1]);
+ while (<OLD>)
+ {
+ chomp();
+ if (/^(\S+)\s+(.+)$/o)
+ {
+ my ($key, $value) = ($1, $2);
+ delete($hash{$key}) if exists($hash{$key});
+ print $_, "\n";
+ }
+ else
+ {
+ print $_, "\n";
+ }
+ }
+
+ # print missing configuration lines
+ for (keys %hash)
+ {
+ print $_, " ", $hash{$_}, "\n";
+ }
+ close(OLD);
+ '
+ perl -e "$script" $confdir/local.cf.$$ $confdir/local.cf > \
+ $confdir/local.cf.$$.$$ && \
+ mv -f $confdir/local.cf.$$.$$ $confdir/local.cf.$$
+
+ rm -f $confdir/local.cf.$$.$$
+ fi
+
+ # set bayes/razor2/dcc in local.cf.$$
+ for i in bayes razor2 dcc; do
+ use_name="use_$i"
+ use_value=$(eval echo \$$use_name)
+ if [ "$use_value" = "true" ]; then
+ use_value=1
+ else
+ use_value=0
+ fi
+ cp_check_and_sed "^$use_name " \
+ "s/^$use_name .*/$use_name $use_value/g" \
+ $confdir/local.cf.$$ || true
+ done
+
+ # different than template
+ if ! cmp -s $confdir/local.cf.$$ $confdir/local.cf; then
+ # backup if not zero size
+ if [ -s $confdir/local.cf ]; then
+ echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
+ cp_backup_conffile $confdir/local.cf
+ fi
+
+ echo "CN: Installing new version of $confdir/local.cf"
+ mv -f $confdir/local.cf.$$ $confdir/local.cf
+
+ restart=yes
+ fi
+
+ rm -f $confdir/local.cf.$$
+fi
+
+# check and replace /etc/default/spamasassin if needed
+if [ "$run_service" = "true" -a \
+ -f $examdir/etc-default-spamassassin-$run_as_user ]; then
+ if ! cmp -s $examdir/etc-default-spamassassin-$run_as_user \
+ /etc/default/spamassassin; then
+ echo "CN: Backing up old version of /etc/default/spamassassin in /var/backups"
+ cp_backup_conffile /etc/default/spamassassin
+
+ echo "CN: Installing new version of /etc/default/spamassassin"
+ cp -pf $examdir/etc-default-spamassassin-$run_as_user \
+ /etc/default/spamassassin
+
+ restart=yes
+ fi
+fi
+
+# do we run spamd at all?
+if [ "$run_service" = "true" ]; then
+ i_run_service=1
+else
+ i_run_service=0
+fi
+
+# enable or disable spamd in /etc/default/spamassassin
+if [ -f "/etc/default/spamassassin" ]; then
+ cp_check_and_sed '^ENABLED=' \
+ "s/^ENABLED=.*/ENABLED=$i_run_service/g" \
+ /etc/default/spamassassin || true
+ if [ "$run_service" = "true" ]; then
+ echo "CN: Will start spamd by default"
+ else
+ echo "CN: Will not start spamd by default"
+ fi
+ restart=yes
+fi
+
+# fix old /razor-agent.log bug
+if [ -f "/razor-agent.log" ]; then
+ mv -f /razor-agent.log /tmp
+ echo "CN: Moving razor logs (razor-agent.log) to /tmp"
+fi
+
+# fix razor agent position
+if [ -f "/etc/razor/razor-agent.conf" ]; then
+ cp_check_and_sed '^.*logfile.*=' \
+ "s,^.*logfile.*=.*,logfile=sys-syslog,g" \
+ /etc/razor/razor-agent.conf || true
+ restart=yes
+fi
+
+# cleanup the AWL
+if getent passwd amavis >/dev/null 2>&1; then
+ if [ -s ~amavis/.spamassassin/auto-whitelist ]; then
+ echo "CN: Cleaning the Amavis auto-whitelist"
+ su -c '/usr/bin/check_whitelist --clean' - amavis >/dev/null 2>&1
+ fi
+fi
+
+# import SA keys
+echo "CN: Importing SpamAssassin and SARE public PGP keys"
+echo "$(date +'%d-%m-%Y %T'): Importing new SA keys..." >> \
+ /var/log/sa-update.log
+if [ ! -d /etc/spamassassin/sa-update-keys ]; then
+ mkdir -p /etc/spamassassin/sa-update-keys
+ touch /etc/spamassassin/sa-update-keys/pubring.gpg \
+ /etc/spamassassin/sa-update-keys/secring.gpg
+ chmod -R go-rwx /etc/spamassassin/sa-update-keys
+fi
+sa-update --import /etc/spamassassin/sa-keys.pgp \
+ >>/var/log/sa-update.log 2>&1 || true
+
+# clean old RulesDuJour rules
+echo "CN: Cleaning old RulesDuJour and SARE rules..."
+rm -rf /etc/spamassassin/*_sare_*.cf /etc/spamassassin/RulesDuJour
+echo "CN: Please use /etc/spamassassin/sa-update.channels in future!"
+
+# get/update the rules
+echo "CN: Downloading new SpamAssassin rules and updating in background"
+echo "$(date +'%d-%m-%Y %T'): Fetching new SA rules..." >> \
+ /var/log/sa-update.log
+sa-update \
+ --channelfile /etc/spamassassin/sa-update.channels \
+ --gpgkey 856AA88A \
+ --gpgkey 24F434CE \
+ --gpgkey BDE9DC10 >>/var/log/sa-update.log 2>&1 || true
+echo "CN: SpamAssassin updates are now logged at /var/log/sa-update.log"
+
+# restart the spamasassin
+if [ "x$restart" = "xyes" ]; then
+ if [ -x /usr/sbin/invoke-rc.d ]; then
+ pkill -f /usr/sbin/spamd || true
+ invoke-rc.d --quiet spamassassin restart
+ else
+ /etc/init.d/spamassassin restart
+ fi
+fi
+
+# restart the amavis
+if [ "x$restart" = "xyes" -a -x /etc/init.d/amavis ]; then
+ if [ -x /usr/sbin/invoke-rc.d ]; then
+ invoke-rc.d --quiet amavis restart
+ else
+ /etc/init.d/amavis restart
+ fi
+fi
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0