cc6be230f4cd0586bf42a9192ffa482e88818123
[spamassassin-cn.git] / debian / postinst
1 #!/bin/sh
2 # postinst script for spamassassin-cn
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
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>
12 #          <new-version>
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
18 #
19
20 case "$1" in
21     configure|reconfigure)
22       # continue below
23     ;;
24
25     *)
26         exit 0
27     ;;
28 esac
29
30 # import CN-functions
31 . /usr/share/carnet-tools/functions.sh
32
33 # load Debconf
34 . /usr/share/debconf/confmodule
35 db_version 2.0
36
37 umask 022
38
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
42 fi
43
44 db_get spamassassin-cn/usebayes || true
45 use_bayes="$RET"
46
47 db_get spamassassin-cn/userazor2 || true
48 use_razor2="$RET"
49
50 db_get spamassassin-cn/runservice || true
51 run_service="$RET"
52
53 db_get spamassassin-cn/runasuser || true
54 run_as_user="$RET"
55
56 db_stop || true 
57
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
64         else
65             /etc/init.d/spamassassin stop
66         fi
67     rstart=yes
68     fi
69
70     if ! getent group spamd >/dev/null 2>&1; then
71         echo "CN: Adding group spamd"
72         addgroup --system spamd >/dev/null 2>&1
73     fi
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
79     fi
80
81     if [ ! -d "/var/run/spamd" ]; then
82         mkdir /var/run/spamd
83     fi
84
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
89
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
96         else
97             /etc/init.d/spamassassin stop
98         fi
99     restart=yes
100     fi
101
102     if [ -d "/var/run/spamd" ]; then
103         rm -rf /var/run/spamd
104     fi
105     dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
106 fi
107
108 examdir="/usr/share/doc/spamassassin-cn/examples"
109 confdir="/etc/spamassassin"
110
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
116     fi
117
118     # old enough
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
124     fi
125
126     # copy template
127     cp -f $examdir/local.cf $confdir/local.cf.$$
128
129     # merge both configurations
130     if [ -s $confdir/local.cf ]; then
131         #cat $confdir/local.cf > $confdir/local.cf.$$
132
133         # merge old and new in one conf, primarily respecting old
134         script='
135         my %confhash = ();
136         my ($key, $value);
137
138         # read template
139         open (NEW, "<", $ARGV[0]);
140         while (<NEW>)
141         {
142           chomp();
143           if (/^(\S+)\s+(.+)$/o)
144           {
145             my ($key, $value) = ($1, $2);
146             $hash{$key} = $value;
147           }
148         }
149         close(NEW);
150
151         # read old configuration
152         open (OLD, "<", $ARGV[1]);
153         while (<OLD>)
154         {
155           chomp();
156           if (/^(\S+)\s+(.+)$/o)
157           {
158             my ($key, $value) = ($1, $2);
159             delete($hash{$key}) if exists($hash{$key});
160                 print $_, "\n";
161           }
162           else
163           {
164             print $_, "\n";
165           }
166         }
167
168         # print missing configuration lines
169         for (keys %hash)
170         {
171           print $_, " ", $hash{$_}, "\n";
172         }
173         close(OLD);
174         '
175         perl -e "$script" $confdir/local.cf.$$ $confdir/local.cf > \
176                 $confdir/local.cf.$$.$$ && \
177             mv -f $confdir/local.cf.$$.$$ $confdir/local.cf.$$
178
179         rm -f $confdir/local.cf.$$.$$
180     fi
181
182     # set bayes/razor2 in local.cf.$$
183     for i in bayes razor2; do
184         use_name="use_$i"
185         use_value=$(eval echo \$$use_name)
186         if [ "$use_value" = "true" ]; then
187             use_value=1
188         else
189             use_value=0
190         fi
191         cp_check_and_sed "^$use_name " \
192             "s/^$use_name .*/$use_name $use_value/g" \
193             $confdir/local.cf.$$ || true
194     done
195
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.$$.$$
201
202     # calculate trusted networks
203     cp_get_netaddr || true
204     netaddr="$RET"
205     # ... get already configured trusted networks
206     trusted_networks=$(awk '/^trusted_networks/ { print $2 }' \
207         $confdir/local.cf.$$ | sed -e 's/,/ /g')
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"
212         fi
213     done
214     # ... merge with Postfix ranges
215     if [ -x /usr/sbin/postconf ]; then
216         for i in $(postconf -h mynetworks | sed -e 's/,/ /g'); do
217             if ! echo "$trusted_networks" | grep -q "$i"; then
218                 trusted_networks="$trusted_networks $i"
219             fi
220         done
221     fi
222     # ... strip 127/8 range
223     for i in $trusted_networks; do
224         if ! echo "$i" | egrep -q \
225             "127.0.0.0/8|\[::1\]/128|\[::ffff:127.0.0.0\]/104"; then
226             trusted_networks_clean="$trusted_networks_clean $i"
227         fi
228     done
229     # ... strip left and right blanks from final string
230     trusted_networks_clean=$(echo "$trusted_networks_clean" \
231         |  sed 's/^[ \t]*//;s/[ \t]*$//')
232     # ... and replace if non-zero
233     if [ ! -z "$trusted_networks_clean" ]; then
234         trusted_networks=$trusted_networks_clean
235     fi
236     cp_check_and_sed "^trusted_networks " \
237         "s#^trusted_networks .*#trusted_networks $trusted_networks#g" \
238         $confdir/local.cf.$$ || true
239     echo "CN: Activated $trusted_networks as trusted networks"
240
241     if dpkg --compare-versions "$2" lt "2:3.3.5"; then
242       cp_check_and_sed "^bayes_auto_expire 1" \
243           "s#^bayes_auto_expire .*#bayes_auto_expire 0#g" \
244           $confdir/local.cf.$$ || true
245       echo "CN: Set bayes_auto_expire to 0"
246     fi
247         
248     # different than template
249     if ! cmp -s $confdir/local.cf.$$ $confdir/local.cf; then
250         # backup if not zero size
251         if [ -s $confdir/local.cf ]; then
252             echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
253             cp_backup_conffile $confdir/local.cf
254         fi
255
256         echo "CN: Installing new version of $confdir/local.cf"
257         mv -f $confdir/local.cf.$$ $confdir/local.cf
258
259         restart=yes
260     fi
261
262     rm -f $confdir/local.cf.$$
263 fi
264
265 # enable/disable plugins in SA
266 list=$(find /etc/spamassassin -type f -name 'v*.pre')
267 for i in $list; do
268     # enable DKIM
269     cp -f $i $i.$$
270     plugin="loadplugin Mail::SpamAssassin::Plugin::DKIM"
271     cp_check_and_sed "^.*#.*$plugin.*$" \
272         "s/^.*#.*$plugin.*$/$plugin/g" $i.$$ || true
273     if ! cmp -s $i.$$ $i; then
274         mv -f $i.$$ $i
275     fi
276     rm -f $i.$$
277
278     # disable Pyzor
279     cp -f $i $i.$$
280     plugin="loadplugin Mail::SpamAssassin::Plugin::Pyzor"
281     cp_check_and_sed "^$plugin.*$" \
282         "s/^$plugin.*$/#$plugin/g" $i.$$ || true
283     if ! cmp -s $i.$$ $i; then
284         mv -f $i.$$ $i
285     fi
286     rm -f $i.$$
287
288     # disable DCC
289     cp -f $i $i.$$
290     plugin="loadplugin Mail::SpamAssassin::Plugin::DCC"
291     cp_check_and_sed "^$plugin.*$" \
292         "s/^$plugin.*$/#$plugin/g" $i.$$ || true
293     if ! cmp -s $i.$$ $i; then
294         mv -f $i.$$ $i
295     fi
296     rm -f $i.$$
297
298     # enable AWL
299     cp -f $i $i.$$
300     plugin="loadplugin Mail::SpamAssassin::Plugin::AWL"
301     cp_check_and_sed "^#$plugin.*$" \
302         "s/^#$plugin.*$/$plugin/g" $i.$$ || true
303     if ! cmp -s $i.$$ $i; then
304         mv -f $i.$$ $i
305     fi
306     rm -f $i.$$
307 done
308
309 # check and replace /etc/default/spamasassin if needed
310 if [ "$run_service" = "true" -a \
311         -f $examdir/etc-default-spamassassin-$run_as_user ]; then
312     if ! cmp -s $examdir/etc-default-spamassassin-$run_as_user \
313             /etc/default/spamassassin; then
314         echo "CN: Backing up old version of /etc/default/spamassassin in /var/backups"
315     cp_backup_conffile /etc/default/spamassassin
316
317     echo "CN: Installing new version of /etc/default/spamassassin"
318     cp -pf $examdir/etc-default-spamassassin-$run_as_user \
319             /etc/default/spamassassin
320
321     restart=yes
322     fi
323 fi
324
325 # do we run spamd at all?
326 if [ "$run_service" = "true" ]; then
327     i_run_service=1
328 else
329     i_run_service=0
330 fi
331
332 # enable or disable spamd in /etc/default/spamassassin
333 if [ -f "/etc/default/spamassassin" ]; then
334     cp_check_and_sed '^ENABLED=' \
335         "s/^ENABLED=.*/ENABLED=$i_run_service/g" \
336         /etc/default/spamassassin || true
337     if [ "$run_service" = "true" ]; then
338         echo "CN: Will start spamd by default"
339     else
340         echo "CN: Will not start spamd by default"
341     fi
342     restart=yes
343 fi
344
345 # fix old /razor-agent.log bug
346 if [ -f "/razor-agent.log" ]; then
347     mv -f /razor-agent.log /tmp
348     echo "CN: Moving razor logs (razor-agent.log) to /tmp"
349 fi
350
351 # fix razor agent position
352 if [ -f "/etc/razor/razor-agent.conf" ]; then
353     cp_check_and_sed '^.*logfile.*=' \
354         "s,^.*logfile.*=.*,logfile=sys-syslog,g" \
355         /etc/razor/razor-agent.conf || true
356     restart=yes
357 fi
358
359 # cleanup the AWL
360 if getent passwd amavis >/dev/null 2>&1; then
361   AWL=~amavis/.spamassassin/auto-whitelist
362   if [ -s $AWL ]; then
363     echo "CN: Cleaning the Amavis auto-whitelist"
364     su -c '/usr/bin/check_whitelist --clean' - amavis >/dev/null 2>&1 \
365       || true
366
367     # see if size is still unacceptable
368     SIZE=$(du -ks $AWL | awk '{print $1}')
369     if [ $SIZE -ge 51200 ]; then
370         echo "CN: Size of AWL database > 50MB, doing full cleanup"
371         cp /dev/null $AWL
372     fi
373   fi
374 fi
375
376 # import SA keys
377 echo "CN: Importing SpamAssassin channels' public PGP keys"
378 echo "$(date +'%d-%m-%Y %T'): Importing new SA keys..." >> \
379     /var/log/sa-update.log
380 if [ ! -d /etc/spamassassin/sa-update-keys ]; then
381     mkdir -p /etc/spamassassin/sa-update-keys
382     touch /etc/spamassassin/sa-update-keys/pubring.gpg \
383         /etc/spamassassin/sa-update-keys/secring.gpg
384     chmod -R go-rwx /etc/spamassassin/sa-update-keys
385 fi
386 sa-update --import /etc/spamassassin/sa-keys.pgp \
387     >>/var/log/sa-update.log 2>&1 || true
388
389 # clean old RulesDuJour rules
390 echo "CN: Cleaning old RulesDuJour and SARE rules..."
391 rm -rf /etc/spamassassin/*_sare_*.cf /etc/spamassassin/RulesDuJour
392 find /var/lib/spamassassin -iname \*_sare_\* -print0| xargs -0r rm -rf
393 sed -i -e '/dostech.net/d' /etc/spamassassin/sa-update.channels
394 echo "CN: Please use /etc/spamassassin/sa-update.channels in future!"
395     
396 # get/update the rules
397 echo "CN: Downloading new SpamAssassin rules and updating in background"
398 echo "$(date +'%d-%m-%Y %T'): Fetching new SA rules..." >> \
399     /var/log/sa-update.log
400 sa-update \
401     --channelfile /etc/spamassassin/sa-update.channels \
402     --allowplugins \
403     --gpgkey 6C6191E3 \
404     --gpgkey 24F434CE \
405     --gpgkey BDE9DC10 >>/var/log/sa-update.log 2>&1 &
406 echo "CN: SpamAssassin updates are now logged at /var/log/sa-update.log"
407
408 # restart the spamasassin
409 if [ "x$restart" = "xyes" ]; then
410     if [ -x /usr/sbin/invoke-rc.d ]; then
411         pkill -f /usr/sbin/spamd || true
412         invoke-rc.d --quiet spamassassin restart
413     else
414         /etc/init.d/spamassassin restart
415     fi
416 fi
417
418 # restart the amavis
419 if [ "x$restart" = "xyes" -a -x /etc/init.d/amavisd-cn ]; then
420     if [ -x /usr/sbin/invoke-rc.d ]; then
421         invoke-rc.d --quiet amavisd-cn restart 
422     else
423         /etc/init.d/amavisd-cn restart
424     fi
425 elif [ "x$restart" = "xyes" -a -x /etc/init.d/amavis ]; then
426     if [ -x /usr/sbin/invoke-rc.d ]; then
427         invoke-rc.d --quiet amavis restart 
428     else
429         /etc/init.d/amavis restart
430     fi
431 fi
432
433 # dh_installdeb will replace this with shell code automatically
434 # generated by other debhelper scripts.
435
436 #DEBHELPER#
437
438 exit 0