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