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 or undo your changes to this 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 '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ]; 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 [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
334 newfile="${newdir}/$(basename "$file" .conf).conf"
335 if [ ! -e "$newfile" ]; then
336 cp_echo "CN: Preserving changes to $newfile (renamed from $file)."
337 cp_mv "$file" "$newfile"
338 if [ -n "$toenable" ]; then
339 cp_echo "CN: Enabling configuration $newfile"
340 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
350 # Append the .conf suffix to all configuration files located in specified
351 # available and enabled directories. Updated symlinks if necessary.
355 local ctype adir edir afile efile newfile
370 if [ -d "${edir}" ] && [ -n "$(ls -A ${edir}/)" ]; then
372 for efile in ${edir}/*; do
373 [ -z "$(echo "$efile" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
375 [ ! -e "${edir}/$(basename "$efile" .conf).conf" ] || continue
377 afile="$(readlink -q -m "$efile")"
379 [ "$(dirname "$afile")" = "$adir" ] || continue
380 [ "$(basename "$afile" .conf)" = "$(basename "$efile" .conf)" ] || continue
382 newfile="${adir}/$(basename "$afile" .conf).conf"
383 [ ! -e "$newfile" ] || continue
385 cp_echo "CN: Preserving changes to $newfile (renamed from $afile)."
386 cp_mv "$afile" "$newfile"
388 cp_echo "CN: Removing obsolete symlink $efile"
391 cp_echo "CN: Enabling configuration $newfile"
392 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
399 # Set trap for deleting all temp files.
401 trap cleanup 0 1 2 15
404 # Backup all configuration located in /etc/apache2/conf.d/,
405 # /etc/apache2/conf-available/ and /etc/apache2/sites-available/
408 if [ -e "$CONF" ]; then
409 cp_echo "CN: Doing backup for $CONF"
410 cp_backup_conffile -d $BACKUPDIR -p $CONF
413 backup_conf $CONFDIR/conf.d
414 backup_conf $CONFDIR/conf-available
415 backup_conf $CONFDIR/sites-available
417 cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
420 # Enable Apache2 web server modules (mpm_prefork, cgi, rewrite, userdir, suexec, php5, ssl).
422 if [ -e "$CONF" ]; then
423 cp_echo "CN: Enabling the prefork Apache2 MPM."
424 if [ "$(a2query -M || true)" != "prefork" ]; then
425 a2dismod -m -q "mpm_$(a2query -M || true)"
426 a2enmod -m -q mpm_prefork
429 cp_echo "CN: Enabling required Apache2 web server modules."
430 a2enmod -m -q access_compat
432 a2enmod -m -q rewrite
433 a2enmod -m -q userdir
440 # Make sure configuration files have the .conf suffix. Move them
441 # to appropriate locations.
443 if [ -d "$CONFDIR/conf.d" ]; then
444 cp_echo "CN: Obsolete configuration directory $CONFDIR/conf.d/ found."
445 move_conf -e conf $CONFDIR/conf.d $CONFDIR/conf-available
448 rename_conf site $CONFDIR/sites-available $CONFDIR/sites-enabled
451 # Check and add IncludeOptional lines to /etc/apache2/apache2.conf:
453 # IncludeOptional conf-enabled/*.conf
454 # IncludeOptional sites-enabled/*.conf
456 if [ -e "$CONF" ]; then
458 cp_echo "CN: Checking IncludeOptional lines in $CONF"
460 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
461 temp_files="${temp_files} ${CONFTMP}"
462 cp "$CONF" "$CONFTMP"
464 sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?conf\.d(/)?$#IncludeOptional conf-enabled/\*\.conf#I' \
466 sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?sites-enabled(/)?$#IncludeOptional sites-enabled/\*\.conf#I' \
469 if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+conf-enabled/\*\.conf$" "$CONFTMP"; then
470 echo 'IncludeOptional conf-enabled/*.conf' >> "$CONFTMP"
472 if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+sites-enabled/\*\.conf$" "$CONFTMP"; then
473 echo 'IncludeOptional sites-enabled/*.conf' >> "$CONFTMP"
476 if ! cmp -s "$CONFTMP" "$CONF"; then
477 cp_mv "$CONFTMP" "$CONF"
484 # Install CARNet specific configuration file.
486 install_conf carnet 000-carnet
488 # Enable SSL port (443).
492 # Disable default site configuration.
494 if [ -e "$CONF" ]; then
495 cp_echo "CN: Disabling default site configuration."
496 a2dissite -m -f -q 000-default || true
501 # Apache2 SSL certificate.
503 if [ -d "$CONFDIR/conf-enabled" ] && [ -n "$(ls -A $CONFDIR/conf-enabled/)" ]; then
504 listen_ssl_mask=$CONFDIR/conf-enabled/*.conf
506 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls -A $CONFDIR/sites-enabled/)" ]; then
507 listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*.conf
510 for file in $CONF $listen_ssl_mask; do
511 if [ -f "$file" ]; then
512 if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
519 if [ $has_listen_ssl -eq 0 ]; then
521 db_get apache2-cn/sslcf || true
524 if [ -n "$apache2_sslcf" ]; then
526 db_get apache2-cn/sslckf || true
527 apache2_sslckf="$RET"
529 db_get apache2-cn/sslccf || true
530 apache2_sslccf="$RET"
535 # Generate new SSL certificate files.
550 db_get apache2-cn/wwwhost || true
551 if [ "$RET" = "true" ]; then
553 # Add WWW VirtualHost.
554 if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
555 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
557 if [ -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ]; then
558 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/www.$DOMAIN.conf
561 chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
562 if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
563 if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
564 install_vhost -d -r www.$DOMAIN default $FQDN 000-$FQDN
566 install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
571 chk_conf_tag "$CONFDIR/sites-available/www.$DOMAIN.conf"
572 if [ ! -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ] || [ $RET -eq 0 ]; then
573 install_vhost default www.$DOMAIN www.$DOMAIN
578 # No WWW VirtualHost.
579 if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
580 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
583 chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
584 if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
585 if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
586 install_vhost -d -r $FQDN default $FQDN 000-$FQDN
588 install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
596 # Add VirtualHost for SSL?
598 if [ $has_listen_ssl -eq 0 ]; then
600 if [ -f "$CONFDIR/sites-available/001-ssl.conf" ]; then
601 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/001-ssl.conf
604 # No active SSL VirtualHosts found - add new one.
605 chk_conf_tag "$CONFDIR/sites-available/001-ssl.conf"
606 if [ ! -f "$CONFDIR/sites-available/001-ssl.conf" ] || [ $RET -eq 0 ]; then
608 db_get apache2-cn/wwwhost || true
609 if [ "$RET" = "true" ]; then
610 install_vhost -r www.$DOMAIN -n $HOST ssl ssl 001-ssl
612 install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
619 # Check SSL certificates location for VirtualHosts.
621 if [ $has_listen_ssl -eq 0 ]; then
623 chk_conf_tag "${CONFDIR}/sites-available/001-ssl.conf"
624 if [ $RET -eq 0 ] && [ -n "$apache2_sslcf" ]; then
626 SSLTMP=$(mktemp ${CONFDIR}/ssltmp.XXXXXX)
627 temp_files="${temp_files} ${SSLTMP} ${SSLTMP}.cn-old"
628 cp ${CONFDIR}/sites-available/001-ssl.conf $SSLTMP
631 cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
632 "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
635 # SSLCertificateKeyFile
636 cp_check_and_sed "^[[:space:]]*SSLCertificateKeyFile \/etc\/ssl\/private\/apache2\.key" \
637 "s#SSLCertificateKeyFile /etc/ssl/private/apache2.key#SSLCertificateKeyFile $apache2_sslckf #g" \
640 # SSLCertificateChainFile
641 if [ -n "$apache2_sslccf" ]; then
642 cp_check_and_sed "^[[:space:]]*# SSLCertificateChainFile \/etc\/ssl\/certs\/(sureserverEDU|cert-chain)\.pem" \
643 "s#\# SSLCertificateChainFile /etc/ssl/certs/\(sureserverEDU\|cert-chain\).pem#SSLCertificateChainFile $apache2_sslccf #g" \
647 cp_mv $SSLTMP ${CONFDIR}/sites-available/001-ssl.conf
652 [ -e "${SSLTMP}" ] && rm -f ${SSLTMP}
653 [ -e "${SSLTMP}.cn-old" ] && rm -f ${SSLTMP}.cn-old
658 # Check file access permissions for SSL certificates.
660 cp_echo "CN: Checking file access permissions for Apache2 SSL certificates."
661 sslkey=/etc/ssl/private
662 sslcerts="${sslkey}/ca.key ${sslkey}/apache2-ca.key ${sslkey}/apache2.key"
663 for certf in $sslcerts; do
664 if [ -f "$certf" ]; then
670 # Check and remove obsolete "Include /etc/apache2/sites-enabled/[^.#]*" from
671 # /etc/apache2/apache2.conf.
673 if egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$" "$CONF"; then
675 cp_echo "CN: Fixing obsolete Include line in $CONF."
676 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
677 temp_files="${temp_files} ${CONFTMP}"
679 sed -r "/^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$/Id" \
682 if ! egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/$" "$CONFTMP"; then
683 echo "Include /etc/apache2/sites-enabled/" >> "$CONFTMP"
686 cp_mv "$CONFTMP" "$CONF"
694 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
696 if [ -e "$CONFDIR/conf.d/aosi-www.conf" ] || [ -e "$CONFDIR/conf.d/aosi.conf" ]; then
697 cp_echo "CN: Removing old AOSI configuration files for Apache2."
698 rm -f $CONFDIR/conf.d/aosi-www.conf
699 rm -f $CONFDIR/conf.d/aosi.conf
704 # Restart Apache2 web server if needed.
706 if [ $need_restart -eq 1 ]; then
708 # Check Apache2 web server configuration.
709 if apache2ctl configtest 2>/dev/null; then
711 # Restart Apache2 web server.
712 service apache2 reload || true
715 # Something is broken.
716 cp_echo "CN: Your Apache2 configuration seems to be broken."
717 cp_echo "CN: Please, check the service after the installation finishes!"
727 # (re)generate monit.d files if monit-cn is installed.
729 if [ -x "/usr/sbin/update-monit.d" ]; then
730 cp_echo "CN: Updating monit configuration..."
731 update-monit.d || true