385320946570e86f29b463ef342ae5c489ec2c47
[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 db_get spamassassin-cn/usebayes || true
40 use_bayes="$RET"
41
42 db_get spamassassin-cn/userazor2 || true
43 use_razor2="$RET"
44
45 db_get spamassassin-cn/runservice || true
46 run_service="$RET"
47
48 db_get spamassassin-cn/runasuser || true
49 run_as_user="$RET"
50
51 db_stop || true 
52
53 # run spamd service as non-root
54 if [ "$run_as_user" = "non-root" ]; then
55     if ! grep -q '^OPTIONS=".*username=spamd"' \
56             /etc/default/spamassassin; then
57         if [ -x /usr/sbin/invoke-rc.d ]; then
58             invoke-rc.d --quiet spamassassin stop
59         else
60             /etc/init.d/spamassassin stop
61         fi
62     rstart=yes
63     fi
64
65     if ! getent group spamd >/dev/null 2>&1; then
66         echo "CN: Adding group spamd"
67         addgroup --system spamd >/dev/null 2>&1
68     fi
69     if ! getent passwd spamd >/dev/null 2>&1; then
70         echo "CN: Adding user spamd"
71         adduser --system --no-create-home --home /etc/spamassassin \
72             --gecos "SpamAssassin User" \
73             --ingroup spamd --disabled-password spamd >/dev/null 2>&1
74     fi
75
76     if [ ! -d "/var/run/spamd" ]; then
77         mkdir /var/run/spamd
78     fi
79
80     chown -R spamd:spamd /var/run/spamd/
81     dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
82     dpkg-statoverride --update --add spamd spamd 755 /var/run/spamd \
83         >/dev/null 2>&1 || true
84
85 # nope, run spamd as root
86 elif [ "$run_as_user" = "root" ]; then
87     if grep -q '^OPTIONS=".*username=spamd"' \
88             /etc/default/spamassassin; then
89         if [ -x /usr/sbin/invoke-rc.d ]; then
90             invoke-rc.d --quiet spamassassin stop
91         else
92             /etc/init.d/spamassassin stop
93         fi
94     restart=yes
95     fi
96
97     if [ -d "/var/run/spamd" ]; then
98         rm -rf /var/run/spamd
99     fi
100     dpkg-statoverride --remove /var/run/spamd >/dev/null 2>&1 || true
101 fi
102
103 examdir="/usr/share/doc/spamassassin-cn/examples"
104 confdir="/etc/spamassassin"
105
106 # check and replace local.cf if needed
107 if [ -f $examdir/local.cf ]; then
108     # non-existant or zero
109     if [ ! -s $confdir/local.cf ]; then
110         rm -f $confdir/local.cf
111     fi
112
113     # old enough
114     if [ -s $confdir/local.cf ] &&
115             dpkg --compare-versions "$2" lt "2:3.0.3-3"; then
116         echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
117         cp_backup_conffile $confdir/local.cf
118         rm -f $confdir/local.cf
119     fi
120
121     # copy template
122     cp -f $examdir/local.cf $confdir/local.cf.$$
123
124     # merge both configurations
125     if [ -s $confdir/local.cf ]; then
126         #cat $confdir/local.cf > $confdir/local.cf.$$
127
128         # merge old and new in one conf, primarily respecting old
129         script='
130         my %confhash = ();
131         my ($key, $value);
132
133         # read template
134         open (NEW, "<", $ARGV[0]);
135         while (<NEW>)
136         {
137           chomp();
138           if (/^(\S+)\s+(.+)$/o)
139           {
140             my ($key, $value) = ($1, $2);
141             $hash{$key} = $value;
142           }
143         }
144         close(NEW);
145
146         # read old configuration
147         open (OLD, "<", $ARGV[1]);
148         while (<OLD>)
149         {
150           chomp();
151           if (/^(\S+)\s+(.+)$/o)
152           {
153             my ($key, $value) = ($1, $2);
154             delete($hash{$key}) if exists($hash{$key});
155                 print $_, "\n";
156           }
157           else
158           {
159             print $_, "\n";
160           }
161         }
162
163         # print missing configuration lines
164         for (keys %hash)
165         {
166           print $_, " ", $hash{$_}, "\n";
167         }
168         close(OLD);
169         '
170         perl -e "$script" $confdir/local.cf.$$ $confdir/local.cf > \
171                 $confdir/local.cf.$$.$$ && \
172             mv -f $confdir/local.cf.$$.$$ $confdir/local.cf.$$
173
174         rm -f $confdir/local.cf.$$.$$
175     fi
176
177     # set bayes/razor2 in local.cf.$$
178     for i in bayes razor2; do
179         use_name="use_$i"
180         use_value=$(eval echo \$$use_name)
181         if [ "$use_value" = "true" ]; then
182             use_value=1
183         else
184             use_value=0
185         fi
186         cp_check_and_sed "^$use_name " \
187             "s/^$use_name .*/$use_name $use_value/g" \
188             $confdir/local.cf.$$ || true
189     done
190
191     # remove obsolete options from old SA
192     egrep -v '^(bayes_use_chi2_combining|ok_languages|pyzor)' \
193         $confdir/local.cf.$$ > $confdir/local.cf.$$.$$ && \
194             mv -f $confdir/local.cf.$$.$$ $confdir/local.cf.$$
195     rm -f $confdir/local.cf.$$.$$
196
197     # different than template
198     if ! cmp -s $confdir/local.cf.$$ $confdir/local.cf; then
199         # backup if not zero size
200         if [ -s $confdir/local.cf ]; then
201             echo "CN: Backing up old version of $confdir/local.cf in /var/backups"
202             cp_backup_conffile $confdir/local.cf
203         fi
204
205         echo "CN: Installing new version of $confdir/local.cf"
206         mv -f $confdir/local.cf.$$ $confdir/local.cf
207
208         restart=yes
209     fi
210
211     rm -f $confdir/local.cf.$$
212 fi
213
214 # enable/disable plugins in SA
215 list=$(find /etc/spamassassin -type f -name 'v*.pre')
216 for i in $list; do
217     # enable DKIM
218     cp -f $i $i.$$
219     plugin="loadplugin Mail::SpamAssassin::Plugin::DKIM"
220     cp_check_and_sed "^.*#.*$plugin.*$" \
221         "s/^.*#.*$plugin.*$/$plugin/g" $i.$$ || true
222     if ! cmp -s $i.$$ $i; then
223         mv -f $i.$$ $i
224     fi
225     rm -f $i.$$
226
227     # disable Pyzor
228     cp -f $i $i.$$
229     plugin="loadplugin Mail::SpamAssassin::Plugin::Pyzor"
230     cp_check_and_sed "^$plugin.*$" \
231         "s/^$plugin.*$/#$plugin/g" $i.$$ || true
232     if ! cmp -s $i.$$ $i; then
233         mv -f $i.$$ $i
234     fi
235     rm -f $i.$$
236
237     # disable DCC
238     cp -f $i $i.$$
239     plugin="loadplugin Mail::SpamAssassin::Plugin::DCC"
240     cp_check_and_sed "^$plugin.*$" \
241         "s/^$plugin.*$/#$plugin/g" $i.$$ || true
242     if ! cmp -s $i.$$ $i; then
243         mv -f $i.$$ $i
244     fi
245     rm -f $i.$$
246 done
247
248 # check and replace /etc/default/spamasassin if needed
249 if [ "$run_service" = "true" -a \
250         -f $examdir/etc-default-spamassassin-$run_as_user ]; then
251     if ! cmp -s $examdir/etc-default-spamassassin-$run_as_user \
252             /etc/default/spamassassin; then
253         echo "CN: Backing up old version of /etc/default/spamassassin in /var/backups"
254     cp_backup_conffile /etc/default/spamassassin
255
256     echo "CN: Installing new version of /etc/default/spamassassin"
257     cp -pf $examdir/etc-default-spamassassin-$run_as_user \
258             /etc/default/spamassassin
259
260     restart=yes
261     fi
262 fi
263
264 # do we run spamd at all?
265 if [ "$run_service" = "true" ]; then
266     i_run_service=1
267 else
268     i_run_service=0
269 fi
270
271 # enable or disable spamd in /etc/default/spamassassin
272 if [ -f "/etc/default/spamassassin" ]; then
273     cp_check_and_sed '^ENABLED=' \
274         "s/^ENABLED=.*/ENABLED=$i_run_service/g" \
275         /etc/default/spamassassin || true
276     if [ "$run_service" = "true" ]; then
277         echo "CN: Will start spamd by default"
278     else
279         echo "CN: Will not start spamd by default"
280     fi
281     restart=yes
282 fi
283
284 # fix old /razor-agent.log bug
285 if [ -f "/razor-agent.log" ]; then
286     mv -f /razor-agent.log /tmp
287     echo "CN: Moving razor logs (razor-agent.log) to /tmp"
288 fi
289
290 # fix razor agent position
291 if [ -f "/etc/razor/razor-agent.conf" ]; then
292     cp_check_and_sed '^.*logfile.*=' \
293         "s,^.*logfile.*=.*,logfile=sys-syslog,g" \
294         /etc/razor/razor-agent.conf || true
295     restart=yes
296 fi
297
298 # cleanup the AWL
299 if getent passwd amavis >/dev/null 2>&1; then
300   if [ -s ~amavis/.spamassassin/auto-whitelist ]; then
301     echo "CN: Cleaning the Amavis auto-whitelist"
302     su -c '/usr/bin/check_whitelist --clean' - amavis >/dev/null 2>&1 \
303       || true
304   fi
305 fi
306
307 # import SA keys
308 echo "CN: Importing SpamAssassin and SARE public PGP keys"
309 echo "$(date +'%d-%m-%Y %T'): Importing new SA keys..." >> \
310     /var/log/sa-update.log
311 if [ ! -d /etc/spamassassin/sa-update-keys ]; then
312     mkdir -p /etc/spamassassin/sa-update-keys
313     touch /etc/spamassassin/sa-update-keys/pubring.gpg \
314         /etc/spamassassin/sa-update-keys/secring.gpg
315     chmod -R go-rwx /etc/spamassassin/sa-update-keys
316 fi
317 sa-update --import /etc/spamassassin/sa-keys.pgp \
318     >>/var/log/sa-update.log 2>&1 || true
319
320 # clean old RulesDuJour rules
321 echo "CN: Cleaning old RulesDuJour and SARE rules..."
322 rm -rf /etc/spamassassin/*_sare_*.cf /etc/spamassassin/RulesDuJour
323 echo "CN: Please use /etc/spamassassin/sa-update.channels in future!"
324     
325 # get/update the rules
326 echo "CN: Downloading new SpamAssassin rules and updating in background"
327 echo "$(date +'%d-%m-%Y %T'): Fetching new SA rules..." >> \
328     /var/log/sa-update.log
329 sa-update \
330     --channelfile /etc/spamassassin/sa-update.channels \
331     --gpgkey 856AA88A \
332     --gpgkey 24F434CE \
333     --gpgkey BDE9DC10 >>/var/log/sa-update.log 2>&1 || true
334 echo "CN: SpamAssassin updates are now logged at /var/log/sa-update.log"
335
336 # restart the spamasassin
337 if [ "x$restart" = "xyes" ]; then
338     if [ -x /usr/sbin/invoke-rc.d ]; then
339         pkill -f /usr/sbin/spamd || true
340         invoke-rc.d --quiet spamassassin restart
341     else
342         /etc/init.d/spamassassin restart
343     fi
344 fi
345
346 # restart the amavis
347 if [ "x$restart" = "xyes" -a -x /etc/init.d/amavis ]; then
348     if [ -x /usr/sbin/invoke-rc.d ]; then
349         invoke-rc.d --quiet amavis restart 
350     else
351         /etc/init.d/amavis restart
352     fi
353 fi
354
355 # dh_installdeb will replace this with shell code automatically
356 # generated by other debhelper scripts.
357
358 #DEBHELPER#
359
360 exit 0