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