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.
109 if [ ! -f "$PORTCONF" ] || ! egrep -iq "^[[:space:]]*Listen[[:space:]]+443$" "$PORTCONF"; then
110 cp_echo "CN: Enabling SSL port (443) for Apache2 web server."
112 out=$(mktemp ${PORTCONF}.XXXXXX)
113 temp_files="${temp_files} ${out}"
115 if [ -f "$PORTCONF" ]; then
119 echo "Listen 443" >> $out
129 # Install specified Apache2 configuration file.
132 conftmpl="$A2CNDIR/$1.conf"
133 conf="$CONFDIR/conf-available/$2.conf"
135 if [ ! -e "$conf" ]; then
136 cp_echo "CN: Generating CARNet specific configuration."
137 cp "$conftmpl" "$conf"
139 cp_echo "CN: $conf already exists, left untouched." 1>&2
142 cp_echo "CN: Enabling CARNet specific configuration."
149 # Install specified VirtualHost for Apache2 web server.
153 # install_vhost [-nvh] [-d] [-s docroot_symlink_dest] template site site-enabled-symlink
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)
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)
172 while echo "x$1" | grep -q '^x-'; do
190 if ! echo "$docroot" | grep -q /; then
191 docroot="/var/www/$docroot"
203 vhosttmpl="$1.template"
207 [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
210 vhostdir=$CONFDIR/sites-available
211 venabledir=$CONFDIR/sites-enabled
213 if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
214 echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
218 [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
220 # if we were broken mid-installation, force
221 if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
225 # add vhost if either of these is true
226 # - adding is forced OR
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}"
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
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"
249 if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
251 echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
252 elif [ -n "$symlink_docroot" ]; then
253 ln -fs "$symlink_docroot" "$docroot"
262 # Backup configuration files located in specified directory.
265 local dir file backup_dir
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"
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.
288 local toenable ctype dir newdir file newfile
290 if [ "$1" = "-e" ]; then
308 if [ -z "$newdir" ]; then
312 if [ -d "${dir}" ] && [ -n "$(ls -A ${dir}/)" ]; then
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
332 # Append the .conf suffix to all configuration files located in specified
333 # available and enabled directories. Updated symlinks if necessary.
337 local ctype adir edir afile efile newfile
352 if [ -d "${edir}" ] && [ -n "$(ls -A ${edir}/)" ]; then
354 for efile in ${edir}/*; do
355 [ -z "$(echo "$efile" | egrep '^/.*(~|(\.(old|staro|bkp|bak|swp|tmp|dpkg-.+|cn-.+)))$')" ] || continue
357 [ ! -e "${edir}/$(basename "$efile" .conf).conf" ] || continue
359 afile="$(readlink -q -m "$efile")"
361 [ "$(dirname "$afile")" = "$adir" ] || continue
362 [ "$(basename "$afile" .conf)" = "$(basename "$efile" .conf)" ] || continue
364 newfile="${adir}/$(basename "$afile" .conf).conf"
365 [ ! -e "$newfile" ] || continue
367 cp_echo "CN: Preserving changes to $newfile (renamed from $afile)."
368 cp_mv "$afile" "$newfile"
370 cp_echo "CN: Removing obsolete symlink $efile"
373 cp_echo "CN: Enabling configuration $newfile"
374 a2en$ctype -m -q "$(basename "$newfile" .conf)" || true
382 # Recursively walks /etc/apache2/apache2.conf for Include and
383 # IncludeOptional directives.
384 # Prints all configfiles so defined.
388 local base_dir="`dirname $1`"
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
394 if [ -e "$i" ]; then echo "`readlink -m -q $i`"; listconffiles "$i"; fi
400 # Set trap for deleting all temp files.
402 trap cleanup 0 1 2 15
405 # Backup all configuration located in /etc/apache2/conf.d/,
406 # /etc/apache2/conf-available/ and /etc/apache2/sites-available/
409 if [ -e "$CONF" ]; then
410 cp_echo "CN: Doing backup for $CONF"
411 cp_backup_conffile -d $BACKUPDIR -p $CONF
414 backup_conf $CONFDIR/conf.d
415 backup_conf $CONFDIR/conf-available
416 backup_conf $CONFDIR/sites-available
418 cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
421 # Enable Apache2 web server modules (mpm_prefork, cgi, rewrite, userdir, suexec, php7.0, ssl).
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
430 cp_echo "CN: Enabling required Apache2 web server modules."
431 a2enmod -m -q access_compat
433 a2enmod -m -q rewrite
434 a2enmod -m -q userdir
441 # Make sure configuration files have the .conf suffix. Move them
442 # to appropriate locations.
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
449 rename_conf site $CONFDIR/sites-available $CONFDIR/sites-enabled
452 # Check and add IncludeOptional lines to /etc/apache2/apache2.conf:
454 # IncludeOptional conf-enabled/*.conf
455 # IncludeOptional sites-enabled/*.conf
457 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"
483 # Remove deprecated directives. Add default Mutex if not defined.
485 if [ -e "$CONF" ]; then
486 ( listconffiles "$CONF"; echo "$CONF" ) | while read -r a2cfile; do
488 a2cfiletmp=`mktemp $a2cfile.tmp.XXXXXX`
489 temp_files="${temp_files} ${a2cfiletmp}"
490 cp "$a2cfile" "$a2cfiletmp"
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' \
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' \
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"
511 if ! cmp -s "$a2cfiletmp" "$a2cfile"; then
512 cp_mv "$a2cfiletmp" "$a2cfile"
520 # Install CARNet specific configuration file.
522 install_conf carnet 000-carnet
524 # Enable SSL port (443).
528 # Disable default site configuration.
530 if [ -e "$CONF" ]; then
531 cp_echo "CN: Disabling default site configuration."
532 a2dissite -m -f -q 000-default || true
537 # Apache2 SSL certificate.
539 if [ -d "$CONFDIR/conf-enabled" ] && [ -n "$(ls -A $CONFDIR/conf-enabled/)" ]; then
540 listen_ssl_mask=$CONFDIR/conf-enabled/*.conf
542 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls -A $CONFDIR/sites-enabled/)" ]; then
543 listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*.conf
546 for file in $CONF $listen_ssl_mask; do
547 if [ -f "$file" ]; then
548 if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
555 if [ $has_listen_ssl -eq 0 ]; then
556 db_get apache2-cn/sslcf || true
559 if [ -n "$apache2_sslcf" ]; then
560 db_get apache2-cn/sslckf || true
561 apache2_sslckf="$RET"
563 db_get apache2-cn/sslccf || true
564 apache2_sslccf="$RET"
568 # Generate new SSL certificate files.
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
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
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
596 install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
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
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
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
617 install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
625 # Add VirtualHost for SSL?
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
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
639 install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
646 # Check SSL certificates location for VirtualHosts.
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
656 cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
657 "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
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" \
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" \
672 cp_mv $SSLTMP ${CONFDIR}/sites-available/001-ssl.conf
677 [ -e "${SSLTMP}" ] && rm -f ${SSLTMP}
678 [ -e "${SSLTMP}.cn-old" ] && rm -f ${SSLTMP}.cn-old
683 # Check file access permissions for SSL certificates.
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
695 # Check and remove obsolete "Include /etc/apache2/sites-enabled/[^.#]*" from
696 # /etc/apache2/apache2.conf.
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}"
703 sed -r "/^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/\[\^\.\#\]\*$/Id" \
706 if ! egrep -iq "^[[:space:]]*Include[[:space:]]+\/etc\/apache2\/sites-enabled\/$" "$CONFTMP"; then
707 echo "Include /etc/apache2/sites-enabled/" >> "$CONFTMP"
710 cp_mv "$CONFTMP" "$CONF"
718 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
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
728 # Restart Apache2 web server if needed.
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
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!"
748 # (re)generate monit.d files if monit-cn is installed.
750 if [ -x "/usr/sbin/update-monit.d" ]; then
751 cp_echo "CN: Updating monit configuration..."
752 update-monit.d || true