f3598f1ae1181612d55586d5f0b968152803c3b0
[amavisd-cn.git] / debian / postinst
1 #!/bin/sh
2 # last update: jelly+paketi@srce.hr  Mon Oct 30 14:37:06 CET 2006
3
4 set -e
5
6 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
7
8 case "$1" in
9   configure)
10     # continue below
11   ;;
12
13   abort-upgrade|abort-remove|abort-deconfigure)
14     exit 0
15   ;;
16
17   *)
18     echo "postinst called with unknown argument \`$1'" >&2
19     exit 0
20   ;;
21 esac
22
23 PATH=/bin:/usr/bin:/sbin:/usr/sbin
24 export PATH
25
26 . /usr/share/amavisd-cn/version.sh
27 . /usr/share/carnet-tools/functions.sh
28 . /usr/share/amavisd-cn/variables.sh
29 . /usr/share/amavisd-cn/functions.sh
30
31 # Place configuration tweaks done on upgrades into this function
32 update_conf() {
33   [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
34   # comment out spam alerts if we're upgrading from
35   # << 20030616p10-4 in woody, or << 2:20030616p10-5 in sarge,
36   # or a fresh installation is taking place
37   if dpkg --compare-versions "$2" lt 20030616p10-4 || \
38     { dpkg --compare-versions "$2" ge 2:0 && \
39       dpkg --compare-versions "$2" lt 2:20030616p10-5; }; then
40     if cp_check_and_sed '^\$spam_admin = "spamalert\\@\$mydomain";$' \
41        's/^\(\$spam_admin\b\)/# \1/' $ACONF; then
42       cp_echo "CN: commented \$spam_admin in $ACONF."
43       cp_echo "CN: Be sure to whitelist that address if you reenable it!"
44       cp_echo "    If spam detection is enabled for that address, loops may occur."
45       restart_daemon=1
46     fi
47   fi
48   # saner defaults - silently discard viruses, and do SMTP-time reject for
49   # explicitely banned attachments instead of bounces
50   if dpkg --compare-versions "$2" lt 2:20030616p10-8; then
51     if cp_check_and_sed \
52       '^[       ]*\$final_virus_destiny[        ]*=[    ]*D_BOUNCE' \
53       's/^\([ \t]*\$final_virus_destiny[ \t]*=[ \t]*\)D_BOUNCE/\1D_DISCARD/' \
54       $ACONF; then
55       cp_echo "CN: Discarding viruses (option \$final_virus_destiny)."
56       restart_daemon=1
57     fi
58     if cp_check_and_sed \
59       '^[       ]*\$final_banned_destiny[       ]*=[    ]*D_BOUNCE' \
60       's/^\([ \t]*\$final_banned_destiny[ \t]*=[ \t]*\)D_BOUNCE/\1D_REJECT/' \
61       $ACONF; then
62       cp_echo "CN: Rejecting banned files at SMTP time (option \$final_banned_destiny)."
63       restart_daemon=1
64     fi
65   fi
66   if dpkg --compare-versions "$2" lt 2:20030616p10-11 && \
67      [ "$domain" != "$host" ]; then
68     if cp_check_and_sed \
69       '^[       ]*\$mydomain[   ]*=[    ]* ["'"']$host['"'"]' \
70       's/^\([ \t]*\$mydomain[ \t]*=[ \t]*\)["'"']$host['"'"]/\1'"'$domain'"/ \
71       $ACONF; then
72       cp_echo "CN: MX for $domain detected, updating \$mydomain."
73       restart_daemon=1
74     fi
75   fi
76 }
77
78 # find out which MTA, assume postfix
79 mta=postfix
80 ACONFTMPL=$POSTTMPL
81 TMPLVERSION=$POSTTMPLVERSION
82 if dpkg -l postfix | grep -q '^.i'; then
83   . /usr/share/amavisd-cn/postfix.sh
84 elif dpkg -l sendmail | grep -q '^.i'; then
85   mta=sendmail
86   ACONFTMPL=$SENDTMPL
87   TMPLVERSION=$SENDTMPLVERSION
88   . /usr/share/amavisd-cn/sendmail.sh
89 else
90   # should never happen, we check for this in preinst too!
91   echo "CN: Ugh, no supported mail-transported-agent could be found?!" >&2
92   echo "CN: If you really have a MTA supported by CARNet installed," >&2
93   echo "CN: Please inform the maintainer.  Assuming ${mta}..." >&2
94 fi 
95
96 # XXX remove at least some of woody cruft for CARNet Debian 2.1+1
97 # convert sweep-cn back to "sweep" account, fix uid/gid
98 if getent passwd sweep > /dev/null; then
99   check_and_update_ugid sweep /etc/sweep /var/lib/sav /var/spool/intercheck /var/log/sweep.log || true
100   # chown stuff I forgot in previous versions
101   if dpkg --compare-versions "$2" lt 20030616p10-3; then
102     chown -R sweep:sweep /var/spool/intercheck /var/log/sweep.log 2> /dev/null || true
103   fi
104   if cp_check_and_sed viruser s/viruser/sweep/ /etc/cron.d/sweep-cn /usr/bin/sophos-ide-update; then
105     did_sweep="sweep "
106   fi
107   if cp_check_and_sed viruser "s/sweep viruser/sweep/g; s/viruser/sweep/g" /etc/samba/smb.conf; then
108     /etc/init.d/samba reload || true
109     did_sweep="${did_sweep}smb.conf "
110   fi
111 fi # sweep
112
113 # get rid of viruser
114 if getent passwd viruser > /dev/null || [ -n "$did_sweep" ]; then
115   # remove viruser account usage
116   echo -n "CN: Removing viruser: "
117   [ "$did_sweep" ] && echo -n "$did_sweep"
118   if cp_check_and_sed '^viruser' s/viruser/clamav/ $ALIASES; then
119     newaliases 2>&1 > /dev/null
120     echo -n "aliases "
121   fi
122   if cp_check_and_sed "User viruser" \
123        s/viruser/clamav/ /etc/clamav/clamd.conf; then
124     clamav_changed=1
125   fi
126   if cp_check_and_sed "DatabaseOwner viruser" \
127        s/viruser/clamav/ /etc/clamav/freshclam.conf; then
128     clamav_changed=1
129   fi
130   if [ -n "$clamav_changed" ]; then
131     # add clamav to amavis group
132     echo -n "c"
133     id clamav | grep -q amavis || adduser clamav amavis > /dev/null
134     echo -n "l"
135     /etc/init.d/clamav-daemon stop > /dev/null || true
136     pkill -9 /usr/sbin/clamd || true
137     echo -n "a"
138     /etc/init.d/clamav-freshclam stop > /dev/null || true
139     pkill -9 /usr/bin/freshclam || true
140     echo -n "m"
141     chown -R clamav:clamav \
142       /var/lib/clamav /var/log/clamav /var/run/clamav || true
143     echo -n "a"
144     # Don't abort if clamav services do not restart.
145     /etc/init.d/clamav-daemon start > /dev/null || failed clamav-daemon
146     /etc/init.d/clamav-freshclam start > /dev/null || failed clamav-freshclam
147     echo -n "v "
148   fi
149   # We'll catch other changes later, just fix user now
150   if cp_check_and_sed '$daemon_user.*viruser' s/viruser/amavis/g $ACONF; then
151     stop_amavisd_now=1
152   fi
153   if getent passwd viruser >/dev/null; then
154     if ls -lnG /var/run/amavis $AHOME |grep -q " $(id -u viruser) " || \
155         pgrep -u viruser -f /usr/sbin/amavis-milter > /dev/null || \
156         pgrep -u viruser amavisd > /dev/null; then
157       stop_amavisd_now=1
158     fi
159   fi
160   if [ -n "$stop_amavisd_now" ]; then
161     echo -n "a"
162     if [ -x /etc/init.d/$mta ]; then
163       /etc/init.d/$mta stop > /dev/null
164     else
165       # shouldn't happen either XXX catch it and send to maintainer?
166       echo -n "iee, no init script for $mta! ignoring... a"
167     fi
168     echo -n "m"
169     if [ -x /etc/init.d/amavisd-new-milter ]; then
170       /etc/init.d/amavisd-new-milter stop > /dev/null
171     fi
172     echo -n "a"
173     pkill -9 -u viruser -f /usr/sbin/amavis-milter || true
174     echo -n "v"
175     /etc/init.d/amavis stop > /dev/null
176     echo -n "i"
177     pkill -9 -u viruser -x amavisd || true
178     chown_ahome=1 # do it later
179     echo -n "s "
180     restart_daemon=1
181     [ $mta = sendmail ] && restart_milter=1 || true
182     restart_mta=1
183   fi
184   if getent passwd viruser >/dev/null; then
185     echo -n "userdel"
186     userdel viruser
187   fi
188   echo "."
189   cp_echo -mailonly "CN: Removed user viruser."
190 fi # viruser
191 # added later
192 if cp_check_and_sed viruser s/viruser/clamav/ \
193    /etc/logrotate.d/clamav-daemon /etc/logrotate.d/clamav-freshclam; then
194   :
195 fi # viruser
196
197 # $domain will be equal to $host if nothing better can be found
198 get_domain
199 domain=$RET
200
201 # sendmail config
202 if [ "$mta" = sendmail ]; then
203   update_sendmail
204   conf_sendmailize
205 fi # end sendmail config
206
207 # postfix config
208 if [ "$mta" = postfix ]; then
209   update_postfix
210   conf_postfixize
211 fi # end postfix config
212
213 # amavisd.conf
214 if [ -f "$ACONFOLD" ]; then
215   cp_echo "CN: Amavisd configuration is now in $ACONF."
216   cp_echo "    Previous location was $ACONFOLD."
217   if [ ! -e "$ACONFMOVED" ]; then
218     mv "$ACONFOLD" "$ACONFMOVED" 
219     cp_echo "    Old file renamed to $ACONFMOVED."
220   fi
221   cp_echo ""
222   cp_echo "CN: If you made any changes to $ACONFOLD, they will NOT be moved"
223   cp_echo "CN: to the new location automatically.  You must update the new file"
224   cp_echo "CN: by yourself, and remove the old file afterwards."
225 elif [ -f "$ACONFMOVED" ]; then
226   cp_echo "CN: Remember to remove the old $ACONFMOVED file."
227 fi
228 if [ -f $ACONF ]; then
229   if grep -q _CN_ $ACONF; then
230     # This is unlikely, actually
231     if cp_check_and_sed "s/_CN_DOMAIN_/$domain/g; s/_CN_HOST_/$domain/g" $ACONF; then
232       restart_daemon=1
233     fi
234   else
235     if egrep -q "^\\\$mydomain = 'example.com'" $ACONF; then
236       # Debian default or lame sysadmin detected, replace it by template
237       conf_from_template
238     elif egrep -q "#CARNet#\\\$mydomain = 'example.com';" $ACONF &&
239        dpkg --compare-versions "$2" eq 2:20030616p5-0; then
240       # CARNet Debian 2.1 (sarge) CDROM installation detected
241       noisy_backup $ACONF
242       conf_from_template
243     else
244       # add other fixups to update_conf() above
245       update_conf $*
246     fi
247   fi
248 fi
249 # nonexistent or empty config
250 if [ ! -f $ACONF -o ! -s $ACONF ]; then
251   # Create fresh config from template
252   conf_from_template
253 fi
254
255 # check for SAVI:
256 # if not there, comment it out, if there, uncomment and restart
257 if ! dpkg -l libsavi-perl bunch-perl-modules-cn 2> /dev/null | \
258        egrep -q '^.i' || \
259    ! [ -f /usr/lib/libsavi.so ]; then
260   if cp_check_and_sed "^\['Sophos SAVI'" \
261      "s/^\(\['Sophos SAVI', ..sophos_savi \]\)/#\1/" $ACONF; then
262     cp_echo "CN: Disabled SAVI::Perl usage in ${ACONF}."
263     cp_echo "    To enable it, run sophos-sweep-update, uncomment and restart amavis."
264   fi
265 else
266   if cp_check_and_sed "^#\['Sophos SAVI'" \
267      "s/^#\(\['Sophos SAVI', ..sophos_savi \]\)/\1/" $ACONF; then
268     cp_echo "CN: Enabled SAVI::Perl usage in ${ACONF}."
269     restart_daemon=1
270   fi
271 fi
272
273 check_and_add_alias virusalert root
274 check_and_add_alias spamalert root
275
276 # touch some required files XXX check if necessary for 2.4
277 if [ ! -f $WLIST ]; then
278   touch $WLIST
279   chown_ahome=1
280 fi
281
282 if [ ! -f $BLIST ]; then
283   touch $BLIST
284   chown_ahome=1
285 fi
286
287 if [ ! -f $AHOME/.spamassassin/user_prefs ] ; then
288   [ -d $AHOME/.spamassassin ] || mkdir -p $AHOME/.spamassassin
289   cat > $AHOME/.spamassassin/user_prefs <<-EEND
290         bayes_path              $AHOME/.spamassassin/bayes
291         bayes_auto_expire       0
292         auto_whitelist_path     $AHOME/.spamassassin/auto-whitelist
293         EEND
294   chown_ahome=1
295 fi
296
297 if [ ! -f $AHOME/.spamassassin/auto-whitelist ] ; then
298   touch $AHOME/.spamassassin/auto-whitelist
299   chown_ahome=1
300 fi
301
302 # Raid over rc2.d
303 if [ -x "/etc/init.d/sendmail" -a -e /etc/rc2.d/S20sendmail ]; then
304   update-rc.d -f sendmail remove >/dev/null 2>/dev/null
305   update-rc.d sendmail defaults 21 19 >/dev/null
306 fi
307 if [ -n "$(find /etc/rc2.d -name S18clam\*)" ]; then
308   update-rc.d -f clamav-daemon remove >/dev/null
309   update-rc.d clamav-daemon defaults 22 18 >/dev/null
310 fi
311
312 # Cleanup and finalization
313 if dpkg --compare-versions "$2" lt 2:20030616p10-4; then
314   update-rc.d -f amavisd remove > /dev/null
315   restart_daemon=1
316   chown_ahome=1
317   # a complicated way to say chmod 750
318   dpkg-statoverride --remove $AHOME > /dev/null || true
319   dpkg-statoverride --update --add amavis amavis 750 $AHOME
320 fi
321
322 if [ -n "$chown_ahome" ]; then
323   # might be slow
324   echo -n "CN: Fixing ownership in /var/*/amavis... "
325   chown -R amavis:amavis $AHOME /var/run/amavis || true
326   echo "done."
327   cp_echo -mailonly "CN: Fixed ownerships in /var/*/amavis."
328 fi
329
330 # kill naughty pyzor descendants
331 if dpkg --compare-versions "$2" lt "2:20030616p10-7" && \
332     pgrep -u amavis -f '/usr/bin/pyzor check' > /dev/null; then
333   /etc/init.d/amavisd-cn stop
334   pkill -9 -u amavis -f '/usr/bin/pyzor check' > /dev/null || true
335   /etc/init.d/amavisd-cn start
336   restart_daemon=
337   restart_mta=
338 fi
339
340 # START AMAVISD
341 # about a half of amavisd-cn script is here
342 if [ "$restart_daemon" -a -x /etc/init.d/amavis.amavisd-new ]; then
343   /etc/init.d/amavis.amavisd-new restart
344 fi
345 # always check that the daemons are running
346 if ! wait_for_fds amavis; then
347   /etc/init.d/amavis.amavisd-new start
348   wait_for_fds amavis
349 fi
350 if [ "$mta" = sendmail ]; then
351   if [ "$restart_daemon" -a -x /etc/init.d/amavisd-new-milter ]; then
352     /etc/init.d/amavisd-new-milter restart
353     restart_mta=1
354   fi
355   # always check that the daemons are running
356   if ! wait_for_fds milter; then
357     /etc/init.d/amavisd-new-milter start
358     wait_for_fds milter
359     restart_mta=1
360   fi
361 elif [ "$restart_mta" ]; then
362   /etc/init.d/$mta restart
363 fi
364
365 # this needs to be updated when $CRONTAB file changes
366 if dpkg --compare-versions "$2" lt "2:20030616p10-4"; then
367   cp_echo ""
368   cp_echo "CN: Deleting temp files older than 1 day every day at 01:35 AM"
369   cp_echo "CN: Deleting spam-mail  older than 7 days every day at 03:15 AM"
370   cp_echo "CN: Deleting virus-mail older than 7 days every day at 04:25 AM"
371   cp_echo "    (can be changed in $CRONTAB)"
372 fi
373 # display this message just once... maybe use debconf instead
374 if dpkg --compare-versions "$2" lt "2:20030616p10-4"; then
375   cp_echo ""
376   cp_echo "CN: To stop, start or restart all of the clamav+amavis+mta components,"
377   cp_echo "CN: use the /etc/init.d/amavisd-cn script."
378 fi
379 if [ "$failed" ]; then
380   cp_echo ""
381   cp_echo "CN: Services $failed failed to restart!"
382   cp_echo "CN: Please check and start manually if needed."
383 fi
384
385 # Upgrade, but no automatically changed config;
386 # warn if new template available
387 if [ -n "$2" -a -z "$changed_config" ] && \
388    dpkg --compare-versions "$2" lt "$TMPLVERSION"; then
389   cp_echo ""
390   cp_echo "CN: It seems you have upgraded this package from version $2."
391   cp_echo "CN: Configuration template for $mta was modified in version ${TMPLVERSION}."
392   cp_echo "    You might want to review the changes, or simply copy the new template and"
393   cp_echo "    and replace the _CN_DOMAIN_ string with an adequate value:"
394   cp_echo "    cp $ACONFTMPL $ACONF"
395   cp_echo "    perl -pi -e 's/_CN_DOMAIN_/$domain/g' $ACONF"
396 fi
397
398 cp_mail $PKG $VERSION