5 [ "$1" = "configure" ] || exit 0
6 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
9 . /usr/share/debconf/confmodule
12 . /usr/share/carnet-tools/functions.sh
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
24 FQDN=$(hostname --fqdn)
25 WEBMASTER="webmaster@$FQDN"
27 BACKUPDIR="/var/backups/apache2-cn"
41 # Cleanup all temp files.
44 if [ -n "$temp_files" ]; then
45 for item in $temp_files; do
46 if [ -e "$item" ]; then
55 # Add CARNet package info lines to config's header.
61 if [ -e "$conf_file" ]; then
62 cat >> $conf_file <<EOF
63 ## Begin - Generated by CARNet package apache2-cn
65 # REMOVE this whole block if you DON'T WANT apache2-cn
66 # to edit or undo your changes to this configuration file.
68 ## End - Generated by CARNet package apache2-cn
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
85 if [ -f "$conf_file" ]; then
86 if egrep -q "^## Begin - Generated by CARNet package apache2-cn$" "$conf_file"; then
96 # Generate Apache2 web server SSL certificate.
99 generate_ssl_output=$($A2CNDIR/carnet-generate-ssl ignore "$FQDN" "$WEBMASTER" "$DOMAIN" 2> /dev/null)
100 cp_echo "$generate_ssl_output"
106 # Check if port 443 is configured in ports.conf file.
110 if [ ! -f "$PORTCONF" ] || ! egrep -iq "^[[:space:]]*Listen[[:space:]]+443$" "$PORTCONF"; then
112 cp_echo "CN: Enabling SSL port (443) for Apache2 web server."
114 out=$(mktemp ${PORTCONF}.XXXXXX)
115 temp_files="${temp_files} ${out}"
117 if [ -f "$PORTCONF" ]; then
121 echo "Listen 443" >> $out
131 # Install specified Apache2 configuration file.
134 conftmpl="$A2CNDIR/$1.conf"
135 conf="$CONFDIR/conf-available/$2.conf"
137 if [ ! -e "$conf" ]; then
138 cp_echo "CN: Generating CARNet specific configuration."
139 cp "$conftmpl" "$conf"
141 cp_echo "CN: $conf already exists, left untouched." 1>&2
144 cp_echo "CN: Enabling CARNet specific configuration."
151 # Install specified VirtualHost for Apache2 web server.
155 # install_vhost [-nvh] [-d] [-s docroot_symlink_dest] template site site-enabled-symlink
157 # -nvh - add NameVirtualHost
158 # -d - mkdir DocumentRoot
159 # -r - set DocumentRoot
160 # -n - set ServerName
161 # -s X - symlink DocumentRoot to X (all in /var/www)
163 # site - host part of ServerName unless -r or -n is used
164 # site-enabled-symlink - name of file/symlink in sites-available/sites-enabled
165 # (without .conf suffix)
174 while echo "x$1" | grep -q '^x-'; do
192 if ! echo "$docroot" | grep -q /; then
193 docroot="/var/www/$docroot"
205 vhosttmpl="$1.template"
209 [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
212 vhostdir=$CONFDIR/sites-available
213 venabledir=$CONFDIR/sites-enabled
215 if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
216 echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
220 [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
222 # if we were broken mid-installation, force
223 if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
227 # add vhost if either of these is true
228 # - adding is forced OR
231 if [ -n "$force_vhost" -o \( ! -e "$vhostdir/$venabled" -a ! -e "$venabledir/$venabled" \) ]; then
233 cp_echo "CN: Adding $vhost VirtualHost."
234 out=$(mktemp $vhostdir/$venabled.XXXXXX)
235 temp_files="${temp_files} ${out}"
240 if [ "$add_namevirthost" ]; then
241 nvh=$(awk -F'[ >]' '/^<VirtualHost/ {print $2}' $TMPLDIR/$vhosttmpl |\
242 sed "s/IPADDR/$MYIP/g")
243 echo "NameVirtualHost $nvh" >> $out
246 sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
247 s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
248 cp_mv $out $vhostdir/$venabled
249 chmod 644 $vhostdir/$venabled
250 a2ensite -m -q "$vsite"
252 if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
254 echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
255 elif [ -n "$symlink_docroot" ]; then
256 ln -fs "$symlink_docroot" "$docroot"
265 # Backup configuration files located in specified directory.
268 local dir file backup_dir
272 if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
273 cp_echo "CN: Doing backup for all files in $dir"
274 for file in ${dir}/*; do
275 if [ -f "$file" ]; then
276 if [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ]; then
277 backup_dir="$BACKUPDIR/$(basename $(dirname "$file"))"
278 cp_backup_conffile -d "$backup_dir" -p "$file"
287 # Move configuration files from one directory to another. The .conf suffix
288 # will be added. Will try to enable the configuration if -e is specified.
291 local toenable ctype dir newdir file newfile
293 if [ "$1" = "-e" ]; then
311 if [ -z "$newdir" ]; then
315 if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
317 for file in ${dir}/*; do
318 [ -z "$(echo "$file" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
319 newfile="${newdir}/$(basename "$file" .conf).conf"
320 if [ ! -e "$newfile" ]; then
321 cp_echo "CN: Preserving changes to $newfile (renamed from $file)."
322 cp_mv "$file" "$newfile"
323 if [ -n "$toenable" ]; then
324 cp_echo "CN: Enabling configuration $newfile"
325 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
335 # Append the .conf suffix to all configuration files located in specified
336 # available and enabled directories. Updated symlinks if necessary.
340 local ctype adir edir afile efile newfile
355 if [ -d "${edir}" ] && [ -n "$(ls -A ${edir}/)" ]; then
357 for efile in ${edir}/*; do
358 [ -z "$(echo "$efile" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
360 [ ! -e "${edir}/$(basename "$efile" .conf).conf" ] || continue
362 afile="$(readlink -q -m "$efile")"
364 [ "$(dirname "$afile")" = "$adir" ] || continue
365 [ "$(basename "$afile" .conf)" = "$(basename "$efile" .conf)" ] || continue
367 newfile="${adir}/$(basename "$afile" .conf).conf"
368 [ ! -e "$newfile" ] || continue
370 cp_echo "CN: Preserving changes to $newfile (renamed from $afile)."
371 cp_mv "$afile" "$newfile"
373 cp_echo "CN: Removing obsolete symlink $efile"
376 cp_echo "CN: Enabling configuration $newfile"
377 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
385 # Recursively walks /etc/apache2/apache2.conf for Include and
386 # IncludeOptional directives.
387 # Prints all configfiles so defined.
391 local base_dir="`dirname $1`"
393 incs=`awk 'tolower($1) ~ /include(optional)?/ { sub("/$","/*",$2); print $2; }' $1`
394 incs=`echo "$incs" | sed -r "s#^([^/])#${base_dir}/\1#"`
395 if [ -n "$incs" ]; then
397 if [ -e "$i" ]; then echo "`readlink -m -q $i`"; listconffiles "$i"; fi
403 # Set trap for deleting all temp files.
405 trap cleanup 0 1 2 15
408 # Backup all configuration located in /etc/apache2/conf.d/,
409 # /etc/apache2/conf-available/ and /etc/apache2/sites-available/
412 if [ -e "$CONF" ]; then
413 cp_echo "CN: Doing backup for $CONF"
414 cp_backup_conffile -d $BACKUPDIR -p $CONF
417 backup_conf $CONFDIR/conf.d
418 backup_conf $CONFDIR/conf-available
419 backup_conf $CONFDIR/sites-available
421 cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
424 # Enable Apache2 web server modules (mpm_prefork, cgi, rewrite, userdir, suexec, php7.0, ssl).
426 if [ -e "$CONF" ]; then
427 cp_echo "CN: Enabling the prefork Apache2 MPM."
428 if [ "$(a2query -M || true)" != "prefork" ]; then
429 a2dismod -m -q "mpm_$(a2query -M || true)"
430 a2enmod -m -q mpm_prefork
433 cp_echo "CN: Enabling required Apache2 web server modules."
434 a2enmod -m -q access_compat
436 a2enmod -m -q rewrite
437 a2enmod -m -q userdir
444 # Make sure configuration files have the .conf suffix. Move them
445 # to appropriate locations.
447 if [ -d "$CONFDIR/conf.d" ]; then
448 cp_echo "CN: Obsolete configuration directory $CONFDIR/conf.d/ found."
449 move_conf -e conf $CONFDIR/conf.d $CONFDIR/conf-available
452 rename_conf site $CONFDIR/sites-available $CONFDIR/sites-enabled
455 # Check and add IncludeOptional lines to /etc/apache2/apache2.conf:
457 # IncludeOptional conf-enabled/*.conf
458 # IncludeOptional sites-enabled/*.conf
460 if [ -e "$CONF" ]; then
462 cp_echo "CN: Checking IncludeOptional lines in $CONF"
464 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
465 temp_files="${temp_files} ${CONFTMP}"
466 cp "$CONF" "$CONFTMP"
468 sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?conf\.d(/)?$#IncludeOptional conf-enabled/\*\.conf#I' \
470 sed -r -i 's#^[[:space:]]*Include(Optional)?[[:space:]]+(/etc/apache2/)?sites-enabled(/)?$#IncludeOptional sites-enabled/\*\.conf#I' \
473 if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+conf-enabled/\*\.conf$" "$CONFTMP"; then
474 echo 'IncludeOptional conf-enabled/*.conf' >> "$CONFTMP"
476 if ! egrep -iq "^[[:space:]]*IncludeOptional[[:space:]]+sites-enabled/\*\.conf$" "$CONFTMP"; then
477 echo 'IncludeOptional sites-enabled/*.conf' >> "$CONFTMP"
480 if ! cmp -s "$CONFTMP" "$CONF"; then
481 cp_mv "$CONFTMP" "$CONF"
487 # Remove deprecated directives. Add default Mutex if not defined.
489 if [ -e "$CONF" ]; then
491 ( listconffiles "$CONF"; echo "$CONF" ) | while read -r a2cfile; do
493 a2cfiletmp=`mktemp $a2cfile.tmp.XXXXXX`
494 temp_files="${temp_files} ${a2cfiletmp}"
495 cp "$a2cfile" "$a2cfiletmp"
497 if egrep -iq "^[[:space:]]*NameVirtualHost[[:space:]]+" "$a2cfiletmp"; then
498 cp_echo "CN: Removing deprecated NameVirtualHost from $a2cfile"
499 sed -r -i '/^[[:space:]]*NameVirtualHost[[:space:]]+/Id' \
503 if egrep -iq "^[[:space:]]*SSLMutex[[:space:]]+" "$a2cfiletmp"; then
504 cp_echo "CN: Removing deprecated SSLMutex from $a2cfile"
505 sed -r -i '/^[[:space:]]*SSLMutex[[:space:]]+/Id' \
509 if [ "$a2cfile" = "$CONF" ]; then
510 if ! egrep -iq "^[[:space:]]*Mutex[[:space:]]+" "$a2cfiletmp"; then
511 cp_echo "CN: Adding default Mutex to $a2cfile"
512 echo 'Mutex file:${APACHE_LOCK_DIR} default' >> "$a2cfiletmp"
516 if ! cmp -s "$a2cfiletmp" "$a2cfile"; then
517 cp_mv "$a2cfiletmp" "$a2cfile"
525 # Install CARNet specific configuration file.
527 install_conf carnet 000-carnet
529 # Enable SSL port (443).
533 # Disable default site configuration.
535 if [ -e "$CONF" ]; then
536 cp_echo "CN: Disabling default site configuration."
537 a2dissite -m -f -q 000-default || true
542 # Apache2 SSL certificate.
544 if [ -d "$CONFDIR/conf-enabled" ] && [ -n "$(ls -A $CONFDIR/conf-enabled/)" ]; then
545 listen_ssl_mask=$CONFDIR/conf-enabled/*.conf
547 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls -A $CONFDIR/sites-enabled/)" ]; then
548 listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*.conf
551 for file in $CONF $listen_ssl_mask; do
552 if [ -f "$file" ]; then
553 if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
560 if [ $has_listen_ssl -eq 0 ]; then
562 db_get apache2-cn/sslcf || true
565 if [ -n "$apache2_sslcf" ]; then
567 db_get apache2-cn/sslckf || true
568 apache2_sslckf="$RET"
570 db_get apache2-cn/sslccf || true
571 apache2_sslccf="$RET"
576 # Generate new SSL certificate files.
591 db_get apache2-cn/wwwhost || true
592 if [ "$RET" = "true" ]; then
594 # Add WWW VirtualHost.
595 if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
596 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
598 if [ -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ]; then
599 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/www.$DOMAIN.conf
602 chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
603 if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
604 if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
605 install_vhost -d -r www.$DOMAIN default $FQDN 000-$FQDN
607 install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
612 chk_conf_tag "$CONFDIR/sites-available/www.$DOMAIN.conf"
613 if [ ! -f "$CONFDIR/sites-available/www.$DOMAIN.conf" ] || [ $RET -eq 0 ]; then
614 install_vhost default www.$DOMAIN www.$DOMAIN
619 # No WWW VirtualHost.
620 if [ -f "$CONFDIR/sites-available/000-$FQDN.conf" ]; then
621 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/000-$FQDN.conf
624 chk_conf_tag "$CONFDIR/sites-available/000-$FQDN.conf"
625 if [ ! -f "$CONFDIR/sites-available/000-$FQDN.conf" ] || [ $RET -eq 0 ]; then
626 if egrep -qi "^[[:space:]]*NameVirtualHost[[:space:]]+\*:80$" "$PORTCONF"; then
627 install_vhost -d -r $FQDN default $FQDN 000-$FQDN
629 install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
637 # Add VirtualHost for SSL?
639 if [ $has_listen_ssl -eq 0 ]; then
641 if [ -f "$CONFDIR/sites-available/001-ssl.conf" ]; then
642 cp_backup_conffile -d $BACKUPDIR/sites-available -p $CONFDIR/sites-available/001-ssl.conf
645 # No active SSL VirtualHosts found - add new one.
646 chk_conf_tag "$CONFDIR/sites-available/001-ssl.conf"
647 if [ ! -f "$CONFDIR/sites-available/001-ssl.conf" ] || [ $RET -eq 0 ]; then
649 db_get apache2-cn/wwwhost || true
650 if [ "$RET" = "true" ]; then
651 install_vhost -r www.$DOMAIN -n $HOST ssl ssl 001-ssl
653 install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
660 # Check SSL certificates location for VirtualHosts.
662 if [ $has_listen_ssl -eq 0 ]; then
664 chk_conf_tag "${CONFDIR}/sites-available/001-ssl.conf"
665 if [ $RET -eq 0 ] && [ -n "$apache2_sslcf" ]; then
667 SSLTMP=$(mktemp ${CONFDIR}/ssltmp.XXXXXX)
668 temp_files="${temp_files} ${SSLTMP} ${SSLTMP}.cn-old"
669 cp ${CONFDIR}/sites-available/001-ssl.conf $SSLTMP
672 cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
673 "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
676 # SSLCertificateKeyFile
677 cp_check_and_sed "^[[:space:]]*SSLCertificateKeyFile \/etc\/ssl\/private\/apache2\.key" \
678 "s#SSLCertificateKeyFile /etc/ssl/private/apache2.key#SSLCertificateKeyFile $apache2_sslckf #g" \
681 # SSLCertificateChainFile
682 if [ -n "$apache2_sslccf" ]; then
683 cp_check_and_sed "^[[:space:]]*# SSLCertificateChainFile \/etc\/ssl\/certs\/(sureserverEDU|cert-chain)\.pem" \
684 "s#\# SSLCertificateChainFile /etc/ssl/certs/\(sureserverEDU\|cert-chain\).pem#SSLCertificateChainFile $apache2_sslccf #g" \
688 cp_mv $SSLTMP ${CONFDIR}/sites-available/001-ssl.conf
693 [ -e "${SSLTMP}" ] && rm -f ${SSLTMP}
694 [ -e "${SSLTMP}.cn-old" ] && rm -f ${SSLTMP}.cn-old
699 # Check file access permissions for SSL certificates.
701 cp_echo "CN: Checking file access permissions for Apache2 SSL certificates."
702 sslkey=/etc/ssl/private
703 sslcerts="${sslkey}/ca.key ${sslkey}/apache2-ca.key ${sslkey}/apache2.key"
704 for certf in $sslcerts; do
705 if [ -f "$certf" ]; then
711 # Check and remove obsolete "Include /etc/apache2/sites-enabled/[^.#]*" from
712 # /etc/apache2/apache2.conf.
714 if egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$" "$CONF"; then
716 cp_echo "CN: Fixing obsolete Include line in $CONF."
717 CONFTMP=`mktemp $CONF.tmp.XXXXXX`
718 temp_files="${temp_files} ${CONFTMP}"
720 sed -r "/^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$/Id" \
723 if ! egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/$" "$CONFTMP"; then
724 echo "Include /etc/apache2/sites-enabled/" >> "$CONFTMP"
727 cp_mv "$CONFTMP" "$CONF"
735 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
737 if [ -e "$CONFDIR/conf.d/aosi-www.conf" ] || [ -e "$CONFDIR/conf.d/aosi.conf" ]; then
738 cp_echo "CN: Removing old AOSI configuration files for Apache2."
739 rm -f $CONFDIR/conf.d/aosi-www.conf
740 rm -f $CONFDIR/conf.d/aosi.conf
745 # Restart Apache2 web server if needed.
747 if [ $need_restart -eq 1 ]; then
749 # Check Apache2 web server configuration.
750 if apache2ctl configtest 2>/dev/null; then
752 # Restart Apache2 web server.
753 service apache2 reload || true
756 # Something is broken.
757 cp_echo "CN: Your Apache2 configuration seems to be broken."
758 cp_echo "CN: Please, check the service after the installation finishes!"
768 # (re)generate monit.d files if monit-cn is installed.
770 if [ -x "/usr/sbin/update-monit.d" ]; then
771 cp_echo "CN: Updating monit configuration..."
772 update-monit.d || true