2 # postinst script for spamassassin-cn
4 # see: dh_installdeb(1)
8 # summary of how this script can be called:
9 # * <postinst> `configure' <most-recently-configured-version>
10 # * <old-postinst> `abort-upgrade' <new version>
11 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
13 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
14 # <failed-install-package> <version> `removing'
15 # <conflicting-package> <version>
16 # for details, see http://www.debian.org/doc/debian-policy/ or
17 # the debian-policy package
21 configure|reconfigure)
31 . /usr/share/carnet-tools/functions.sh
34 . /usr/share/debconf/confmodule
39 # clean up old debconf questions that are no longer in this package
40 if dpkg --compare-versions "$2" lt "2:3.1.7-4"; then
41 db_unregister spamassassin-cn/usedcc || true
44 db_get spamassassin-cn/usebayes || true
47 db_get spamassassin-cn/userazor2 || true
50 db_get spamassassin-cn/runservice || true
53 db_get spamassassin-cn/runasuser || true
58 # run spamd service as non-root
59 if [ "$run_as_user" = "non-root" ]; then
60 if ! grep -q '^OPTIONS=".*username=spamd"' \
61 /etc/default/spamassassin; then
62 if [ -x /usr/sbin/invoke-rc.d ]; then
63 invoke-rc.d --quiet spamassassin stop
65 /etc/init.d/spamassassin stop
70 if ! getent group spamd >/dev/null 2>&1; then
71 echo "CN: Adding group spamd"
72 addgroup --system spamd >/dev/null 2>&1
74 if ! getent passwd spamd >/dev/null 2>&1; then
75 echo "CN: Adding user spamd"
76 adduser --system --no-create-home --home /etc/spamassassin \
77 --gecos "SpamAssassin User" \
78 --ingroup spamd --disabled-password spamd >/dev/null 2>&1
81 if [ ! -d "/var/run/spamd" ]; then
85 chown -R spamd:spamd /var/run/spamd/
86 dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
87 dpkg-statoverride --update --add spamd spamd 755 /var/run/spamd \
88 >/dev/null 2>&1 || true
90 # nope, run spamd as root
91 elif [ "$run_as_user" = "root" ]; then
92 if grep -q '^OPTIONS=".*username=spamd"' \
93 /etc/default/spamassassin; then
94 if [ -x /usr/sbin/invoke-rc.d ]; then
95 invoke-rc.d --quiet spamassassin stop
97 /etc/init.d/spamassassin stop
102 if [ -d "/var/run/spamd" ]; then
103 rm -rf /var/run/spamd
105 dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
108 examdir="/usr/share/doc/spamassassin-cn/examples"
109 confdir="/etc/spamassassin"
111 # check and replace local.cf if needed
112 if [ -f $examdir/local.cf ]; then
113 # non-existant or zero
114 if [ ! -s $confdir/local.cf ]; then
115 rm -f $confdir/local.cf
119 if [ -s $confdir/local.cf ] &&
120 dpkg --compare-versions "$2" lt "2:3.2.5-4"; then
121 echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
122 cp_backup_conffile $confdir/local.cf
123 rm -f $confdir/local.cf
127 cp -f $examdir/local.cf $confdir/local.cf.$$
129 # merge both configurations
130 if [ -s $confdir/local.cf ]; then
131 #cat $confdir/local.cf > $confdir/local.cf.$$
133 # merge old and new in one conf, primarily respecting old
139 open (NEW, "<", $ARGV[0]);
143 if (/^(\S+)\s+(.+)$/o)
145 my ($key, $value) = ($1, $2);
146 $hash{$key} = $value;
151 # read old configuration
152 open (OLD, "<", $ARGV[1]);
156 if (/^(\S+)\s+(.+)$/o)
158 my ($key, $value) = ($1, $2);
159 delete($hash{$key}) if exists($hash{$key});
168 # print missing configuration lines
171 print $_, " ", $hash{$_}, "\n";
175 perl -e "$script" $confdir/local.cf.$$ $confdir/local.cf > \
176 $confdir/local.cf.$$.$$ && \
177 mv -f $confdir/local.cf.$$.$$ $confdir/local.cf.$$
179 rm -f $confdir/local.cf.$$.$$
182 # set bayes/razor2 in local.cf.$$
183 for i in bayes razor2; do
185 use_value=$(eval echo \$$use_name)
186 if [ "$use_value" = "true" ]; then
191 cp_check_and_sed "^$use_name " \
192 "s/^$use_name .*/$use_name $use_value/g" \
193 $confdir/local.cf.$$ || true
196 # remove obsolete options from old SA
197 egrep -v '^(bayes_use_chi2_combining|ok_languages|use_pyzor|pyzor_|use_dcc|dcc_)' \
198 $confdir/local.cf.$$ > $confdir/local.cf.$$.$$ && \
199 mv -f $confdir/local.cf.$$.$$ $confdir/local.cf.$$
200 rm -f $confdir/local.cf.$$.$$
202 # calculate trusted networks
203 cp_get_netaddr || true
205 # ... get already configured trusted networks
206 trusted_networks=$(awk '/^trusted_networks/ { print $2 }' \
207 $confdir/local.cf.$$)
208 # ... merge with local networks configured on eth interfaces
209 for i in $netaddr; do
210 if ! echo "$trusted_networks" | grep -q "$i"; then
211 trusted_networks="$trusted_networks $i"
214 # ... merge with Postfix ranges
215 if [ -x /usr/sbin/postconf ]; then
216 for i in $(postconf -h mynetworks); do
217 if ! echo "$trusted_networks" | grep -q "$i"; then
218 trusted_networks="$trusted_networks $i"
222 # ... strip 127/8 range
223 for i in $trusted_networks; do
224 if ! echo "$i" | grep -q "127.0.0.0/8"; then
225 trusted_networks_clean="$trusted_networks_clean $i"
228 trusted_networks_clean=$(echo "$trusted_networks_clean" \
229 | sed 's/^[ \t]*//;s/[ \t]*$//')
230 # ... and replace if non-zero
231 if [ ! -z "$trusted_networks_clean" ]; then
232 trusted_networks=$trusted_networks_clean
234 cp_check_and_sed "^trusted_networks " \
235 "s#^trusted_networks .*#trusted_networks $trusted_networks#g" \
236 $confdir/local.cf.$$ || true
237 echo "CN: Activated $trusted_networks as trusted networks"
239 # different than template
240 if ! cmp -s $confdir/local.cf.$$ $confdir/local.cf; then
241 # backup if not zero size
242 if [ -s $confdir/local.cf ]; then
243 echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
244 cp_backup_conffile $confdir/local.cf
247 echo "CN: Installing new version of $confdir/local.cf"
248 mv -f $confdir/local.cf.$$ $confdir/local.cf
253 rm -f $confdir/local.cf.$$
256 # enable/disable plugins in SA
257 list=$(find /etc/spamassassin -type f -name 'v*.pre')
261 plugin="loadplugin Mail::SpamAssassin::Plugin::DKIM"
262 cp_check_and_sed "^.*#.*$plugin.*$" \
263 "s/^.*#.*$plugin.*$/$plugin/g" $i.$$ || true
264 if ! cmp -s $i.$$ $i; then
271 plugin="loadplugin Mail::SpamAssassin::Plugin::Pyzor"
272 cp_check_and_sed "^$plugin.*$" \
273 "s/^$plugin.*$/#$plugin/g" $i.$$ || true
274 if ! cmp -s $i.$$ $i; then
281 plugin="loadplugin Mail::SpamAssassin::Plugin::DCC"
282 cp_check_and_sed "^$plugin.*$" \
283 "s/^$plugin.*$/#$plugin/g" $i.$$ || true
284 if ! cmp -s $i.$$ $i; then
290 # check and replace /etc/default/spamasassin if needed
291 if [ "$run_service" = "true" -a \
292 -f $examdir/etc-default-spamassassin-$run_as_user ]; then
293 if ! cmp -s $examdir/etc-default-spamassassin-$run_as_user \
294 /etc/default/spamassassin; then
295 echo "CN: Backing up old version of /etc/default/spamassassin in /var/backups"
296 cp_backup_conffile /etc/default/spamassassin
298 echo "CN: Installing new version of /etc/default/spamassassin"
299 cp -pf $examdir/etc-default-spamassassin-$run_as_user \
300 /etc/default/spamassassin
306 # do we run spamd at all?
307 if [ "$run_service" = "true" ]; then
313 # enable or disable spamd in /etc/default/spamassassin
314 if [ -f "/etc/default/spamassassin" ]; then
315 cp_check_and_sed '^ENABLED=' \
316 "s/^ENABLED=.*/ENABLED=$i_run_service/g" \
317 /etc/default/spamassassin || true
318 if [ "$run_service" = "true" ]; then
319 echo "CN: Will start spamd by default"
321 echo "CN: Will not start spamd by default"
326 # fix old /razor-agent.log bug
327 if [ -f "/razor-agent.log" ]; then
328 mv -f /razor-agent.log /tmp
329 echo "CN: Moving razor logs (razor-agent.log) to /tmp"
332 # fix razor agent position
333 if [ -f "/etc/razor/razor-agent.conf" ]; then
334 cp_check_and_sed '^.*logfile.*=' \
335 "s,^.*logfile.*=.*,logfile=sys-syslog,g" \
336 /etc/razor/razor-agent.conf || true
341 if getent passwd amavis >/dev/null 2>&1; then
342 AWL=~amavis/.spamassassin/auto-whitelist
344 echo "CN: Cleaning the Amavis auto-whitelist"
345 su -c '/usr/bin/check_whitelist --clean' - amavis >/dev/null 2>&1 \
348 # see if size is still unacceptable
349 SIZE=$(du -ks $AWL | awk '{print $1}')
350 if [ $SIZE -ge 51200 ]; then
351 echo "CN: Size of AWL database > 50MB, doing full cleanup"
358 echo "CN: Importing SpamAssassin and SARE public PGP keys"
359 echo "$(date +'%d-%m-%Y %T'): Importing new SA keys..." >> \
360 /var/log/sa-update.log
361 if [ ! -d /etc/spamassassin/sa-update-keys ]; then
362 mkdir -p /etc/spamassassin/sa-update-keys
363 touch /etc/spamassassin/sa-update-keys/pubring.gpg \
364 /etc/spamassassin/sa-update-keys/secring.gpg
365 chmod -R go-rwx /etc/spamassassin/sa-update-keys
367 sa-update --import /etc/spamassassin/sa-keys.pgp \
368 >>/var/log/sa-update.log 2>&1 || true
370 # clean old RulesDuJour rules
371 echo "CN: Cleaning old RulesDuJour and SARE rules..."
372 rm -rf /etc/spamassassin/*_sare_*.cf /etc/spamassassin/RulesDuJour
373 echo "CN: Please use /etc/spamassassin/sa-update.channels in future!"
375 # get/update the rules
376 echo "CN: Downloading new SpamAssassin rules and updating in background"
377 echo "$(date +'%d-%m-%Y %T'): Fetching new SA rules..." >> \
378 /var/log/sa-update.log
380 --channelfile /etc/spamassassin/sa-update.channels \
383 --gpgkey BDE9DC10 >>/var/log/sa-update.log 2>&1 || true
384 echo "CN: SpamAssassin updates are now logged at /var/log/sa-update.log"
386 # restart the spamasassin
387 if [ "x$restart" = "xyes" ]; then
388 if [ -x /usr/sbin/invoke-rc.d ]; then
389 pkill -f /usr/sbin/spamd || true
390 invoke-rc.d --quiet spamassassin restart
392 /etc/init.d/spamassassin restart
397 if [ "x$restart" = "xyes" -a -x /etc/init.d/amavis ]; then
398 if [ -x /usr/sbin/invoke-rc.d ]; then
399 invoke-rc.d --quiet amavis restart
401 /etc/init.d/amavis restart
405 # dh_installdeb will replace this with shell code automatically
406 # generated by other debhelper scripts.