Enable php 7.3.
[apache2-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 Debconf
9 . /usr/share/debconf/confmodule
10
11 # Load CARNET Tools
12 . /usr/share/carnet-tools/functions.sh
13
14 PKG="apache2-cn"
15 VERSION="2.4.10+1"
16 CONFDIR="/etc/apache2"
17 CONF="$CONFDIR/apache2.conf"
18 PORTCONF="$CONFDIR/ports.conf"
19 A2CNDIR=/usr/share/apache2-cn
20 TMPLDIR=$A2CNDIR/templates
21 CERTDIR=/etc/ssl/certs
22
23 HOST=$(hostname)
24 FQDN=$(hostname --fqdn)
25 WEBMASTER="webmaster@$FQDN"
26 DOMAIN=$(hostname -d)
27 BACKUPDIR="/var/backups/apache2-cn"
28
29 need_restart=0
30 apache2_sslcf=
31 apache2_sslckf=
32 apache2_sslccf=
33 has_vhosts=0
34 temp_files=
35 has_listen_ssl=0
36 listen_ssl_mask=
37
38
39 # cleanup()
40 #
41 #   Cleanup all temp files.
42 #
43 cleanup () {
44   if [ -n "$temp_files" ]; then
45     for item in $temp_files; do
46       if [ -e "$item" ]; then
47         rm -f $item
48       fi
49     done
50   fi
51 }
52
53 # tag_conf()
54 #
55 #   Add CARNET package info lines to config's header.
56 #
57 tag_conf () {
58   local conf_file
59   conf_file="$1"
60
61   if [ -e "$conf_file" ]; then
62    cat >> $conf_file <<EOF
63 ## Begin - Generated by CARNET package apache2-cn
64 #
65 #  REMOVE this whole block if you DON'T WANT apache2-cn
66 #  to edit or undo your changes to this configuration file.
67 #
68 ## End - Generated by CARNET package apache2-cn
69 EOF
70   fi
71 }
72
73 # chk_conf_tag ()
74 #
75 #   Check if configuration file has CARNET package info lines.
76 #   return:  $RET => 0 - tagged
77 #                    1 - not tagged or file does not exists
78 #                    2 - file exists, but it is not tagged
79 #
80 chk_conf_tag () {
81   local conf_file
82   conf_file="$1"
83   RET=1
84
85   if [ -f "$conf_file" ]; then
86     if egrep -q "^## Begin - Generated by CARNET package apache2-cn$" "$conf_file"; then
87       RET=0
88     else
89       RET=2
90     fi
91   fi
92 }
93
94 # generate_ssl()
95 #
96 #   Generate Apache2 web server SSL certificate.
97 #
98 generate_ssl () {
99   generate_ssl_output=$($A2CNDIR/carnet-generate-ssl ignore "$FQDN" "$WEBMASTER" "$DOMAIN" 2> /dev/null)
100   cp_echo "$generate_ssl_output"
101   need_restart=1
102 }
103
104 # listen_ssl()
105 #
106 #   Check if port 443 is configured in ports.conf file.
107 #
108 listen_ssl() {
109   if [ ! -f "$PORTCONF" ] || ! egrep -iq "^[[:space:]]*Listen[[:space:]]+443$" "$PORTCONF"; then
110     cp_echo "CN: Enabling SSL port (443) for Apache2 web server."
111
112     out=$(mktemp ${PORTCONF}.XXXXXX)
113     temp_files="${temp_files} ${out}"
114
115     if [ -f "$PORTCONF" ]; then
116       cp $PORTCONF $out
117     fi
118
119     echo "Listen 443" >> $out
120     cp_mv $out $PORTCONF
121     chmod 644 $PORTCONF
122
123     need_restart=1
124   fi
125 }
126
127 # install_conf()
128 #
129 #   Install specified Apache2 configuration file.
130 #
131 install_conf() {
132   conftmpl="$A2CNDIR/$1.conf"
133   conf="$CONFDIR/conf-available/$2.conf"
134
135   if [ ! -e "$conf" ]; then
136     cp_echo "CN: Generating CARNET specific configuration."
137     cp "$conftmpl" "$conf"
138   else
139     cp_echo "CN: $conf already exists, left untouched." 1>&2
140   fi
141
142   cp_echo "CN: Enabling CARNET specific configuration."
143   a2enconf -m -q "$2"
144   need_restart=1
145 }
146
147 # install_vhost()
148 #
149 #   Install specified VirtualHost for Apache2 web server.
150 #
151 #   Invocation:
152 #
153 #   install_vhost [-nvh] [-d] [-s docroot_symlink_dest] template site site-enabled-symlink
154 #
155 #     -nvh - add NameVirtualHost
156 #     -d   - mkdir DocumentRoot
157 #     -r   - set DocumentRoot
158 #     -n   - set ServerName
159 #     -s X - symlink DocumentRoot to X (all in /var/www)
160 #
161 #   site - host part of ServerName unless -r or -n is used
162 #   site-enabled-symlink - name of file/symlink in sites-available/sites-enabled
163 #                          (without .conf suffix)
164 #
165 install_vhost() {
166   add_namevirthost=
167   mkdir_docroot=
168   symlink_docroot=
169   docroot=
170   vhostname=
171
172   while echo "x$1" | grep -q '^x-'; do
173     case "$1" in
174       -nvh)
175         add_namevirthost=1
176         shift
177         ;;
178       -d)
179         mkdir_docroot=1
180         shift
181         ;;
182       -s)
183         shift
184         symlink_docroot="$1"
185         shift
186         ;;
187       -r)
188         shift
189         docroot="$1"
190         if ! echo "$docroot" | grep -q /; then
191             docroot="/var/www/$docroot"
192         fi
193         shift
194         ;;
195       -n)
196         shift
197         vhostname="$1"
198         shift
199         ;;
200     esac
201   done
202
203   vhosttmpl="$1.template"
204   vhost="$2"
205   vsite="$3"
206   venabled="$3.conf"
207   [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
208   force_vhost=
209
210   vhostdir=$CONFDIR/sites-available
211   venabledir=$CONFDIR/sites-enabled
212
213   if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
214     echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
215     exit 2
216   fi
217
218   [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
219
220   # if we were broken mid-installation, force
221   if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
222     force_vhost=1
223   fi
224
225   # add vhost if either of these is true
226   # - adding is forced OR
227   # - it doesn't exist
228   #
229   if [ -n "$force_vhost" -o \( ! -e "$vhostdir/$venabled" -a ! -e "$venabledir/$venabled" \) ]; then
230     cp_echo "CN: Adding $vhost VirtualHost."
231     out=$(mktemp $vhostdir/$venabled.XXXXXX)
232     temp_files="${temp_files} ${out}"
233
234     # CARNET header.
235     tag_conf "$out"
236
237     if [ "$add_namevirthost" ]; then
238       nvh=$(awk -F'[ >]' '/^<VirtualHost/ {print $2}' $TMPLDIR/$vhosttmpl |\
239         sed "s/IPADDR/$MYIP/g")
240       echo "NameVirtualHost $nvh" >> $out
241     fi
242
243     sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
244         s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
245     cp_mv $out $vhostdir/$venabled
246     chmod 644 $vhostdir/$venabled
247     a2ensite -m -q "$vsite"
248
249     if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
250       mkdir "$docroot"
251       echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
252     elif [ -n "$symlink_docroot" ]; then
253       ln -fs "$symlink_docroot" "$docroot"
254     fi
255
256     need_restart=1
257   fi
258 }
259
260 # backup_conf()
261 #
262 #   Backup configuration files located in specified directory.
263 #
264 backup_conf () {
265   local dir file backup_dir
266
267   dir="$1"
268
269   if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
270     cp_echo "CN: Doing backup for all files in $dir"
271     for file in ${dir}/*; do
272       if [ -f "$file" ]; then
273         if [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ]; then
274           backup_dir="$BACKUPDIR/$(basename $(dirname "$file"))"
275           cp_backup_conffile -d "$backup_dir" -p "$file"
276         fi
277       fi
278     done
279   fi
280 }
281
282 # move_conf()
283 #
284 #   Move configuration files from one directory to another. The .conf suffix
285 #   will be added. Will try to enable the configuration if -e is specified.
286 #
287 move_conf () {
288   local toenable ctype dir newdir file newfile
289
290   if [ "$1" = "-e" ]; then
291     toenable="$1"
292     shift
293   fi
294
295   ctype="$1"
296   dir="$2"
297   newdir="$3"
298
299   case "$ctype" in
300     site|conf)
301       # continue below
302       ;;
303     *)
304       return 1
305       ;;
306   esac
307
308   if [ -z "$newdir" ]; then
309     newdir="$dir"
310   fi
311
312   if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
313     mkdir -p "$newdir"
314     for file in ${dir}/*; do
315       [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
316       newfile="${newdir}/$(basename "$file" .conf).conf"
317       if [ ! -e "$newfile" ]; then
318         cp_echo "CN: Preserving changes to $newfile (renamed from $file)."
319         cp_mv "$file" "$newfile"
320         if [ -n "$toenable" ]; then
321           cp_echo "CN: Enabling configuration $newfile"
322           a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
323         fi
324         need_restart=1
325       fi
326     done
327   fi
328 }
329
330 # rename_conf()
331 #
332 #   Append the .conf suffix to all configuration files located in specified
333 #   available and enabled directories. Updated symlinks if necessary.
334 #
335 #
336 rename_conf () {
337   local ctype adir edir afile efile newfile
338
339   ctype="$1"
340   adir="$2"
341   edir="$3"
342
343   case "$ctype" in
344     site|conf)
345       # continue below
346       ;;
347     *)
348       return 1
349       ;;
350   esac
351
352   if [ -d "${edir}" ] && [ -n "$(ls -A ${edir}/)" ]; then
353     mkdir -p "$adir"
354     for efile in ${edir}/*; do
355       [ -z "$(echo "$efile" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
356
357       [ ! -e "${edir}/$(basename "$efile" .conf).conf" ] || continue
358
359       afile="$(readlink -q -m "$efile")"
360
361       [ "$(dirname "$afile")" = "$adir" ] || continue
362       [ "$(basename "$afile" .conf)" = "$(basename "$efile" .conf)" ] || continue
363
364       newfile="${adir}/$(basename "$afile" .conf).conf"
365       [ ! -e "$newfile" ] || continue
366
367       cp_echo "CN: Preserving changes to $newfile (renamed from $afile)."
368       cp_mv "$afile" "$newfile"
369
370       cp_echo "CN: Removing obsolete symlink $efile"
371       rm -f "$efile"
372
373       cp_echo "CN: Enabling configuration $newfile"
374       a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
375       need_restart=1
376     done
377   fi
378 }
379
380 # listconffiles()
381 #
382 #   Recursively walks /etc/apache2/apache2.conf for Include and
383 #   IncludeOptional directives.
384 #   Prints all configfiles so defined.
385 #
386 listconffiles () {
387   local i incs
388   local base_dir="`dirname $1`"
389
390   incs=`awk 'tolower($1) ~ /include(optional)?/ { sub("/$","/*",$2); print $2; }' $1`
391   incs=`echo "$incs" | sed -r "s#^([^/])#${base_dir}/\1#"`
392   if [ -n "$incs" ]; then
393     for i in $incs; do
394       if [ -e "$i" ]; then echo "`readlink -m -q $i`"; listconffiles "$i"; fi
395     done
396   fi
397 }
398
399
400 # Set trap for deleting all temp files.
401 #
402 trap cleanup 0 1 2 15
403
404
405 # Backup all configuration located in /etc/apache2/conf.d/,
406 # /etc/apache2/conf-available/ and /etc/apache2/sites-available/
407 # directories.
408 #
409 if [ -e "$CONF" ]; then
410   cp_echo "CN: Doing backup for $CONF"
411   cp_backup_conffile -d $BACKUPDIR -p $CONF
412 fi
413
414 backup_conf $CONFDIR/conf.d
415 backup_conf $CONFDIR/conf-available
416 backup_conf $CONFDIR/sites-available
417
418 cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
419
420
421 # Enable Apache2 web server modules (mpm_prefork, cgi, rewrite, userdir, suexec, php7.3, ssl).
422 #
423 if [ -e "$CONF" ]; then
424   cp_echo "CN: Enabling the prefork Apache2 MPM."
425   if [ "$(a2query -M || true)" != "prefork" ]; then
426     a2dismod -m -q "mpm_$(a2query -M || true)"
427     a2enmod -m -q mpm_prefork
428   fi
429
430   cp_echo "CN: Enabling required Apache2 web server modules."
431   a2enmod -m -q access_compat
432   a2enmod -m -q cgi
433   a2enmod -m -q rewrite
434   a2enmod -m -q userdir
435   a2enmod -m -q suexec
436   a2enmod -m -q php7.3
437   a2enmod -m -q ssl
438 fi
439
440
441 # Make sure configuration files have the .conf suffix. Move them
442 # to appropriate locations.
443 #
444 if [ -d "$CONFDIR/conf.d" ]; then
445   cp_echo "CN: Obsolete configuration directory $CONFDIR/conf.d/ found."
446   move_conf -e conf $CONFDIR/conf.d $CONFDIR/conf-available
447 fi
448
449 rename_conf site $CONFDIR/sites-available $CONFDIR/sites-enabled
450
451
452 # Check and add IncludeOptional lines to /etc/apache2/apache2.conf:
453 #
454 #   IncludeOptional conf-enabled/*.conf
455 #   IncludeOptional sites-enabled/*.conf
456 #
457 if [ -e "$CONF" ]; then
458   cp_echo "CN: Checking IncludeOptional lines in $CONF"
459
460   CONFTMP=`mktemp $CONF.tmp.XXXXXX`
461   temp_files="${temp_files} ${CONFTMP}"
462   cp "$CONF" "$CONFTMP"
463
464   sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?conf\.d(/)?$#IncludeOptional conf-enabled/\*\.conf#I' \
465       "$CONFTMP"
466   sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?sites-enabled(/)?$#IncludeOptional sites-enabled/\*\.conf#I' \
467       "$CONFTMP"
468
469   if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+conf-enabled/\*\.conf$" "$CONFTMP"; then
470     echo 'IncludeOptional conf-enabled/*.conf' >> "$CONFTMP"
471   fi
472   if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+sites-enabled/\*\.conf$" "$CONFTMP"; then
473     echo 'IncludeOptional sites-enabled/*.conf' >> "$CONFTMP"
474   fi
475
476   if ! cmp -s "$CONFTMP" "$CONF"; then
477     cp_mv "$CONFTMP" "$CONF"
478     need_restart=1
479   fi
480   rm -f "$CONFTMP"
481 fi
482
483 # Remove deprecated directives. Add default Mutex if not defined.
484 #
485 if [ -e "$CONF" ]; then
486   ( listconffiles "$CONF"; echo "$CONF" ) | while read -r a2cfile; do
487
488     a2cfiletmp=`mktemp $a2cfile.tmp.XXXXXX`
489     temp_files="${temp_files} ${a2cfiletmp}"
490     cp "$a2cfile" "$a2cfiletmp"
491
492     if egrep -iq "^[[:space:]]*NameVirtualHost[[:space:]]+" "$a2cfiletmp"; then
493       cp_echo "CN: Removing deprecated NameVirtualHost from $a2cfile"
494       sed -r -i '/^[[:space:]]*NameVirtualHost[[:space:]]+/Id' \
495           "$a2cfiletmp"
496     fi
497
498     if egrep -iq "^[[:space:]]*SSLMutex[[:space:]]+" "$a2cfiletmp"; then
499       cp_echo "CN: Removing deprecated SSLMutex from $a2cfile"
500       sed -r -i '/^[[:space:]]*SSLMutex[[:space:]]+/Id' \
501           "$a2cfiletmp"
502     fi
503
504     if [ "$a2cfile" = "$CONF" ]; then
505       if ! egrep -iq "^[[:space:]]*Mutex[[:space:]]+" "$a2cfiletmp"; then
506         cp_echo "CN: Adding default Mutex to $a2cfile"
507         echo 'Mutex file:${APACHE_LOCK_DIR} default' >> "$a2cfiletmp"
508       fi
509     fi
510
511     if ! cmp -s "$a2cfiletmp" "$a2cfile"; then
512       cp_mv "$a2cfiletmp" "$a2cfile"
513       need_restart=1
514     fi
515     rm -f "$a2cfiletmp"
516   done
517 fi
518
519
520 # Install CARNET specific configuration file.
521 #
522 install_conf carnet 000-carnet
523
524 # Enable SSL port (443).
525 #
526 listen_ssl
527
528 # Disable default site configuration.
529 #
530 if [ -e "$CONF" ]; then
531   cp_echo "CN: Disabling default site configuration."
532   a2dissite -m -f -q 000-default || true
533   need_restart=1
534 fi
535
536
537 # Apache2 SSL certificate.
538 #
539 if [ -d "$CONFDIR/conf-enabled" ] && [ -n "$(ls -A $CONFDIR/conf-enabled/)" ]; then
540   listen_ssl_mask=$CONFDIR/conf-enabled/*.conf
541 fi
542 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls -A $CONFDIR/sites-enabled/)" ]; then
543   listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*.conf
544 fi
545
546 for file in $CONF $listen_ssl_mask; do
547   if [ -f "$file" ]; then
548     if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
549       has_listen_ssl=1
550       break
551     fi
552   fi
553 done
554
555 if [ $has_listen_ssl -eq 0 ]; then
556   db_get apache2-cn/sslcf || true
557   apache2_sslcf="$RET"
558
559   if [ -n "$apache2_sslcf" ]; then
560     db_get apache2-cn/sslckf || true
561     apache2_sslckf="$RET"
562
563     db_get apache2-cn/sslccf || true
564     apache2_sslccf="$RET"
565
566     need_restart=1
567   else
568     # Generate new SSL certificate files.
569     generate_ssl
570
571     apache2_sslcf=
572     apache2_sslckf=
573     apache2_sslccf=
574   fi
575 fi
576
577 # Add VirtualHosts.
578 # - on fresh install
579 #
580 if [ -z "$2" ]; then
581   db_get apache2-cn/wwwhost || true
582   if [ "$RET" = "true" ]; then
583     # Add WWW VirtualHost.
584     if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
585       cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
586     fi
587     if [ -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ]; then
588       cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/www.$DOMAIN.conf
589     fi
590
591     chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
592     if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
593       if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
594         install_vhost -d -r www.$DOMAIN default $FQDN 000-$FQDN
595       else
596         install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
597       fi
598       need_restart=1
599     fi
600
601     chk_conf_tag "$CONFDIR/sites-available/www.$DOMAIN.conf"
602     if [ ! -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ] || [ $RET -eq 0 ]; then
603       install_vhost default www.$DOMAIN www.$DOMAIN
604       need_restart=1
605     fi
606   else
607     # No WWW VirtualHost.
608     if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
609         cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
610     fi
611
612     chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
613     if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
614       if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
615         install_vhost -d -r $FQDN default $FQDN 000-$FQDN
616       else
617         install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
618       fi
619       need_restart=1
620     fi
621   fi
622 fi
623
624
625 # Add VirtualHost for SSL?
626 #
627 if [ $has_listen_ssl -eq 0 ]; then
628   if [ -f "$CONFDIR/sites-available/001-ssl.conf" ]; then
629     cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/001-ssl.conf
630   fi
631
632   # No active SSL VirtualHosts found - add new one.
633   chk_conf_tag "$CONFDIR/sites-available/001-ssl.conf"
634   if [ ! -f "$CONFDIR/sites-available/001-ssl.conf" ] || [ $RET -eq 0 ]; then
635     db_get apache2-cn/wwwhost || true
636     if [ "$RET" = "true" ]; then
637       install_vhost -r www.$DOMAIN -n $HOST ssl ssl 001-ssl
638     else
639       install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
640     fi
641     need_restart=1
642   fi
643 fi
644
645
646 # Check SSL certificates location for VirtualHosts.
647 #
648 if [ $has_listen_ssl -eq 0 ]; then
649   chk_conf_tag "${CONFDIR}/sites-available/001-ssl.conf"
650   if [ $RET -eq 0 ] && [ -n "$apache2_sslcf" ]; then
651     SSLTMP=$(mktemp ${CONFDIR}/ssltmp.XXXXXX)
652     temp_files="${temp_files} ${SSLTMP} ${SSLTMP}.cn-old"
653     cp ${CONFDIR}/sites-available/001-ssl.conf $SSLTMP
654
655     # SSLCertificateFile
656     cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
657                      "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
658                      $SSLTMP || true
659
660     # SSLCertificateKeyFile
661     cp_check_and_sed "^[[:space:]]*SSLCertificateKeyFile \/etc\/ssl\/private\/apache2\.key" \
662                      "s#SSLCertificateKeyFile /etc/ssl/private/apache2.key#SSLCertificateKeyFile $apache2_sslckf #g" \
663                      $SSLTMP || true
664
665     # SSLCertificateChainFile
666     if [ -n "$apache2_sslccf" ]; then
667       cp_check_and_sed "^[[:space:]]*# SSLCertificateChainFile \/etc\/ssl\/certs\/(sureserverEDU|cert-chain)\.pem" \
668                        "s#\# SSLCertificateChainFile /etc/ssl/certs/\(sureserverEDU\|cert-chain\).pem#SSLCertificateChainFile $apache2_sslccf #g" \
669                        $SSLTMP || true
670     fi
671
672     cp_mv $SSLTMP ${CONFDIR}/sites-available/001-ssl.conf
673
674     need_restart=1
675
676     # Just to be sure.
677     [ -e "${SSLTMP}" ] && rm -f ${SSLTMP}
678     [ -e "${SSLTMP}.cn-old" ] && rm -f ${SSLTMP}.cn-old
679   fi
680 fi
681
682
683 # Check file access permissions for SSL certificates.
684 #
685 cp_echo "CN: Checking file access permissions for Apache2 SSL certificates."
686 sslkey=/etc/ssl/private
687 sslcerts="${sslkey}/ca.key ${sslkey}/apache2-ca.key ${sslkey}/apache2.key"
688 for certf in $sslcerts; do
689   if [ -f "$certf" ]; then
690     chmod 600 $certf
691   fi
692 done
693
694
695 # Check and remove obsolete "Include /etc/apache2/sites-enabled/[^.#]*" from
696 # /etc/apache2/apache2.conf.
697 #
698 if egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$" "$CONF"; then
699   cp_echo "CN: Fixing obsolete Include line in $CONF."
700   CONFTMP=`mktemp $CONF.tmp.XXXXXX`
701   temp_files="${temp_files} ${CONFTMP}"
702
703   sed -r "/^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$/Id" \
704       "$CONF" > "$CONFTMP"
705
706   if ! egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/$" "$CONFTMP"; then
707     echo "Include /etc/apache2/sites-enabled/" >> "$CONFTMP"
708   fi
709
710   cp_mv "$CONFTMP" "$CONF"
711   need_restart=1
712 fi
713
714
715 db_stop || true
716
717
718 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
719 #
720 if [ -e "$CONFDIR/conf.d/aosi-www.conf" ] || [ -e "$CONFDIR/conf.d/aosi.conf" ]; then
721   cp_echo "CN: Removing old AOSI configuration files for Apache2."
722   rm -f $CONFDIR/conf.d/aosi-www.conf
723   rm -f $CONFDIR/conf.d/aosi.conf
724   need_restart=1
725 fi
726
727
728 # Restart Apache2 web server if needed.
729 #
730 if [ $need_restart -eq 1 ]; then
731     # Check Apache2 web server configuration.
732   if apache2ctl configtest 2>/dev/null; then
733     # Restart Apache2 web server.
734     service apache2 reload || true
735   else
736     # Something is broken.
737     cp_echo "CN: Your Apache2 configuration seems to be broken."
738     cp_echo "CN: Please, check the service after the installation finishes!"
739   fi
740 fi
741
742
743 # Mail root
744 #
745 cp_mail "$PKG"
746
747
748 # (re)generate monit.d files if monit-cn is installed.
749 #
750 if [ -x "/usr/sbin/update-monit.d" ]; then
751   cp_echo "CN: Updating monit configuration..."
752   update-monit.d || true
753 fi
754
755 #DEBHELPER#
756
757 exit 0