5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
7 # Source debconf library.
8 . /usr/share/debconf/confmodule
15 abort-upgrade|abort-remove|abort-deconfigure)
20 echo "postinst called with unknown argument \`$1'" >&2
26 # Include CARNet functions.
27 . /usr/share/carnet-tools/functions.sh
31 CONFDIR="/etc/apache2"
32 CONF="$CONFDIR/apache2.conf"
33 PORTCONF="$CONFDIR/ports.conf"
34 A2CNDIR=/usr/share/apache2-cn
35 TMPLDIR=$A2CNDIR/templates
36 CERTDIR=/etc/ssl/certs
39 FQDN=$(hostname --fqdn)
40 WEBMASTER="webmaster@$FQDN"
42 BACKUPDIR="/var/backups/apache2-cn"
56 # Cleanup all temp files.
59 if [ -n "$temp_files" ]; then
60 for item in $temp_files; do
61 if [ -e "$item" ]; then
70 # Add CARNet package info lines to config's header.
76 if [ -e "$conf_file" ]; then
77 cat >> $conf_file <<EOF
78 ## Begin - Generated by CARNet package apache2-cn
80 # REMOVE this whole block if you DON'T WANT apache2-cn
81 # to edit your configuration file.
83 ## End - Generated by CARNet package apache2-cn
90 # Check if configuration file has CARNet package info lines.
91 # return: $RET => 0 - tagged
92 # 1 - not tagged or file does not exists
93 # 2 - file exists, but it is not tagged
100 if [ -f "$conf_file" ]; then
101 if egrep -q "^## Begin - Generated by CARNet package apache2-cn$" "$conf_file"; then
111 # Generate Apache2 web server SSL certificate.
114 generate_ssl_output=$($A2CNDIR/carnet-generate-ssl ignore "$FQDN" "$WEBMASTER" "$DOMAIN" 2> /dev/null)
115 cp_echo "$generate_ssl_output"
121 # Check if port 443 is configured in ports.conf file.
125 if [ ! -f "$PORTCONF" ] || ! egrep -iq "^[[:space:]]*Listen[[:space:]]+443$" "$PORTCONF"; then
127 cp_echo "CN: Enabling SSL port (443) for Apache2 web server."
129 out=$(mktemp ${PORTCONF}.XXXXXX)
130 temp_files="${temp_files} ${out}"
132 if [ -f "$PORTCONF" ]; then
136 echo "Listen 443" >> $out
146 # Install specified Apache2 configuration file.
149 conftmpl="$A2CNDIR/$1.conf"
150 conf="$CONFDIR/conf-available/$2.conf"
152 if [ ! -e "$conf" ]; then
153 cp_echo "CN: Generating CARNet specific configuration."
154 cp "$conftmpl" "$conf"
156 cp_echo "CN: $conf already exists, left untouched." 1>&2
159 cp_echo "CN: Enabling CARNet specific configuration."
166 # Install specified VirtualHost for Apache2 web server.
170 # install_vhost [-nvh] [-d] [-s docroot_symlink_dest] template site site-enabled-symlink
172 # -nvh - add NameVirtualHost
173 # -d - mkdir DocumentRoot
174 # -r - set DocumentRoot
175 # -n - set ServerName
176 # -s X - symlink DocumentRoot to X (all in /var/www)
178 # site - host part of ServerName unless -r or -n is used
179 # site-enabled-symlink - name of file/symlink in sites-available/sites-enabled
180 # (without .conf suffix)
189 while echo "x$1" | grep -q '^x-'; do
207 if ! echo "$docroot" | grep -q /; then
208 docroot="/var/www/$docroot"
220 vhosttmpl="$1.template"
224 [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
227 vhostdir=$CONFDIR/sites-available
228 venabledir=$CONFDIR/sites-enabled
230 if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
231 echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
235 [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
237 # if we were broken mid-installation, force
238 if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
242 # add vhost if either of these is true
243 # - adding is forced OR
246 if [ -n "$force_vhost" -o \( ! -e "$vhostdir/$venabled" -a ! -e "$venabledir/$venabled" \) ]; then
248 cp_echo "CN: Adding $vhost VirtualHost."
249 out=$(mktemp $vhostdir/$venabled.XXXXXX)
250 temp_files="${temp_files} ${out}"
255 if [ "$add_namevirthost" ]; then
256 nvh=$(awk -F'[ >]' '/^<VirtualHost/ {print $2}' $TMPLDIR/$vhosttmpl |\
257 sed "s/IPADDR/$MYIP/g")
258 echo "NameVirtualHost $nvh" >> $out
261 sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
262 s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
263 cp_mv $out $vhostdir/$venabled
264 chmod 644 $vhostdir/$venabled
265 a2ensite -m -q "$vsite"
267 if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
269 echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
270 elif [ -n "$symlink_docroot" ]; then
271 ln -fs "$symlink_docroot" "$docroot"
280 # Backup configuration files located in specified directory.
283 local dir file backup_dir
287 if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
288 cp_echo "CN: Doing backup for all files in $dir"
289 for file in ${dir}/*; do
290 if [ -f "$file" ]; then
291 if [ -z "$(echo $file | egrep '^/.*~')" ]; then
292 backup_dir="$BACKUPDIR/$(basename $(dirname "$file"))"
293 cp_backup_conffile -d "$backup_dir" -p "$file"
302 # Move configuration files from one directory to another. The .conf suffix
303 # will be added. Will try to enable the configuration if -e is specified.
306 local toenable ctype dir newdir file newfile
308 if [ "$1" = "-e" ]; then
326 if [ -z "$newdir" ]; then
330 if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
332 for file in ${dir}/*; do
333 newfile="${newdir}/$(basename "$file" .conf).conf"
334 if [ ! -e "$newfile" ]; then
335 cp_echo "CN: Preserving changes to $newfile (renamed from $file)."
336 cp_mv "$file" "$newfile"
337 if [ -n "$toenable" ]; then
338 cp_echo "CN: Enabling configuration $newfile"
339 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
349 # Append the .conf suffix to all configuration files located in specified
350 # available and enabled directories. Updated symlinks if necessary.
354 local ctype adir edir afile efile newfile
369 if [ -d "${edir}" ] && [ -n "$(ls -A ${edir}/)" ]; then
371 for efile in ${edir}/*; do
372 [ ! -e "${edir}/$(basename "$efile" .conf).conf" ] || continue
374 afile="$(readlink -q -m "$efile")"
376 [ "$(dirname "$afile")" = "$adir" ] || continue
377 [ "$(basename "$afile" .conf)" = "$(basename "$efile" .conf)" ] || continue
379 newfile="${adir}/$(basename "$afile" .conf).conf"
380 [ ! -e "$newfile" ] || continue
382 cp_echo "CN: Preserving changes to $newfile (renamed from $afile)."
383 cp_mv "$afile" "$newfile"
385 cp_echo "CN: Removing obsolete symlink $efile"
388 cp_echo "CN: Enabling configuration $newfile"
389 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
396 # Set trap for deleting all temp files.
398 trap cleanup 0 1 2 15
401 # Backup all configuration located in /etc/apache2/conf.d/,
402 # /etc/apache2/conf-available/ and /etc/apache2/sites-available/
405 if [ -e "$CONF" ]; then
406 cp_echo "CN: Doing backup for $CONF"
407 cp_backup_conffile -d $BACKUPDIR -p $CONF
410 backup_conf $CONFDIR/conf.d
411 backup_conf $CONFDIR/conf-available
412 backup_conf $CONFDIR/sites-available
414 cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
417 # Enable Apache2 web server modules (mpm_prefork, cgi, rewrite, userdir, suexec, php5, ssl).
419 if [ -e "$CONF" ]; then
420 cp_echo "CN: Enabling the prefork Apache2 MPM."
421 if [ "$(a2query -M || true)" != "prefork" ]; then
422 a2dismod -m -q "mpm_$(a2query -M || true)"
423 a2enmod -m -q mpm_prefork
426 cp_echo "CN: Enabling required Apache2 web server modules."
428 a2enmod -m -q rewrite
429 a2enmod -m -q userdir
436 # Make sure configuration files have the .conf suffix. Move them
437 # to appropriate locations.
439 if [ -d "$CONFDIR/conf.d" ]; then
440 cp_echo "CN: Obsolete configuration directory $CONFDIR/conf.d/ found."
441 move_conf -e conf $CONFDIR/conf.d $CONFDIR/conf-available
444 rename_conf site $CONFDIR/sites-available $CONFDIR/sites-enabled
447 # Check and add IncludeOptional lines to /etc/apache2/apache2.conf:
449 # IncludeOptional conf-enabled/*.conf
450 # IncludeOptional sites-enabled/*.conf
452 if [ -e "$CONF" ]; then
454 cp_echo "CN: Checking IncludeOptional lines in $CONF"
456 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
457 temp_files="${temp_files} ${CONFTMP}"
458 cp "$CONF" "$CONFTMP"
460 sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?conf\.d(/)?$#IncludeOptional conf-enabled/\*\.conf#I' \
462 sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?sites-enabled(/)?$#IncludeOptional sites-enabled/\*\.conf#I' \
465 if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+conf-enabled/\*\.conf$" "$CONFTMP"; then
466 echo 'IncludeOptional conf-enabled/*.conf' >> "$CONFTMP"
468 if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+sites-enabled/\*\.conf$" "$CONFTMP"; then
469 echo 'IncludeOptional sites-enabled/*.conf' >> "$CONFTMP"
472 if ! cmp -s "$CONFTMP" "$CONF"; then
473 cp_mv "$CONFTMP" "$CONF"
480 # Install CARNet specific configuration file.
482 install_conf carnet 000-carnet
484 # Enable SSL port (443).
488 # Disable default site configuration.
490 if [ -e "$CONF" ]; then
491 cp_echo "CN: Disabling default site configuration."
492 a2dissite -m -f -q 000-default || true
497 # Apache2 SSL certificate.
499 if [ -d "$CONFDIR/conf-enabled" ] && [ -n "$(ls -A $CONFDIR/conf-enabled/)" ]; then
500 listen_ssl_mask=$CONFDIR/conf-enabled/*.conf
502 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls -A $CONFDIR/sites-enabled/)" ]; then
503 listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*.conf
506 for file in $CONF $listen_ssl_mask; do
507 if [ -f "$file" ]; then
508 if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
515 if [ $has_listen_ssl -eq 0 ]; then
517 db_get apache2-cn/sslcf || true
520 if [ -n "$apache2_sslcf" ]; then
522 db_get apache2-cn/sslckf || true
523 apache2_sslckf="$RET"
525 db_get apache2-cn/sslccf || true
526 apache2_sslccf="$RET"
531 # Generate new SSL certificate files.
546 db_get apache2-cn/wwwhost || true
547 if [ "$RET" = "true" ]; then
549 # Add WWW VirtualHost.
550 if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
551 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
553 if [ -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ]; then
554 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/www.$DOMAIN.conf
557 chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
558 if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
559 if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
560 install_vhost -d -r www.$DOMAIN default $FQDN 000-$FQDN
562 install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
567 chk_conf_tag "$CONFDIR/sites-available/www.$DOMAIN.conf"
568 if [ ! -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ] || [ $RET -eq 0 ]; then
569 install_vhost default www.$DOMAIN www.$DOMAIN
574 # No WWW VirtualHost.
575 if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
576 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
579 chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
580 if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
581 if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
582 install_vhost -d -r $FQDN default $FQDN 000-$FQDN
584 install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
592 # Add VirtualHost for SSL?
594 if [ $has_listen_ssl -eq 0 ]; then
596 if [ -f "$CONFDIR/sites-available/001-ssl.conf" ]; then
597 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/001-ssl.conf
600 # No active SSL VirtualHosts found - add new one.
601 chk_conf_tag "$CONFDIR/sites-available/001-ssl.conf"
602 if [ ! -f "$CONFDIR/sites-available/001-ssl.conf" ] || [ $RET -eq 0 ]; then
604 db_get apache2-cn/wwwhost || true
605 if [ "$RET" = "true" ]; then
606 install_vhost -r www.$DOMAIN -n $HOST ssl ssl 001-ssl
608 install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
615 # Check SSL certificates location for VirtualHosts.
617 if [ $has_listen_ssl -eq 0 ]; then
619 chk_conf_tag "${CONFDIR}/sites-available/001-ssl.conf"
620 if [ $RET -eq 0 ] && [ -n "$apache2_sslcf" ]; then
622 SSLTMP=$(mktemp ${CONFDIR}/ssltmp.XXXXXX)
623 temp_files="${temp_files} ${SSLTMP} ${SSLTMP}.cn-old"
624 cp ${CONFDIR}/sites-available/001-ssl.conf $SSLTMP
627 cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
628 "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
631 # SSLCertificateKeyFile
632 cp_check_and_sed "^[[:space:]]*SSLCertificateKeyFile \/etc\/ssl\/private\/apache2\.key" \
633 "s#SSLCertificateKeyFile /etc/ssl/private/apache2.key#SSLCertificateKeyFile $apache2_sslckf #g" \
636 # SSLCertificateChainFile
637 if [ -n "$apache2_sslccf" ]; then
638 cp_check_and_sed "^[[:space:]]*# SSLCertificateChainFile \/etc\/ssl\/certs\/(sureserverEDU|cert-chain)\.pem" \
639 "s#\# SSLCertificateChainFile /etc/ssl/certs/\(sureserverEDU\|cert-chain\).pem#SSLCertificateChainFile $apache2_sslccf #g" \
643 cp_mv $SSLTMP ${CONFDIR}/sites-available/001-ssl.conf
648 [ -e "${SSLTMP}" ] && rm -f ${SSLTMP}
649 [ -e "${SSLTMP}.cn-old" ] && rm -f ${SSLTMP}.cn-old
654 # Check file access permissions for SSL certificates.
656 cp_echo "CN: Checking file access permissions for Apache2 SSL certificates."
657 sslkey=/etc/ssl/private
658 sslcerts="${sslkey}/ca.key ${sslkey}/apache2-ca.key ${sslkey}/apache2.key"
659 for certf in $sslcerts; do
660 if [ -f "$certf" ]; then
666 # Check and remove obsolete "Include /etc/apache2/sites-enabled/[^.#]*" from
667 # /etc/apache2/apache2.conf.
669 if egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$" "$CONF"; then
671 cp_echo "CN: Fixing obsolete Include line in $CONF."
672 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
673 temp_files="${temp_files} ${CONFTMP}"
675 sed -r "/^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$/Id" \
678 if ! egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/$" "$CONFTMP"; then
679 echo "Include /etc/apache2/sites-enabled/" >> "$CONFTMP"
682 cp_mv "$CONFTMP" "$CONF"
690 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
692 if [ -e "$CONFDIR/conf.d/aosi-www.conf" ] || [ -e "$CONFDIR/conf.d/aosi.conf" ]; then
693 cp_echo "CN: Removing old AOSI configuration files for Apache2."
694 rm -f $CONFDIR/conf.d/aosi-www.conf
695 rm -f $CONFDIR/conf.d/aosi.conf
700 # Restart Apache2 web server if needed.
702 if [ $need_restart -eq 1 ]; then
704 # Check Apache2 web server configuration.
705 if apache2ctl configtest 2>/dev/null; then
707 # Restart Apache2 web server.
708 service apache2 reload || true
711 # Something is broken.
712 cp_echo "CN: Your Apache2 configuration seems to be broken."
713 cp_echo "CN: Please, check the service after the installation finishes!"
723 # (re)generate monit.d files if monit-cn is installed.
725 if [ -x "/usr/sbin/update-monit.d" ]; then
726 cp_echo "CN: Updating monit configuration..."
727 update-monit.d || true