Initial commit
[apache2-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
6
7 # Source debconf library.
8 . /usr/share/debconf/confmodule
9
10 case "$1" in
11         configure)
12                 # continue below
13                 ;;
14
15         abort-upgrade|abort-remove|abort-deconfigure)
16                 exit 0
17                 ;;
18
19         *)
20                 echo "postinst called with unknown argument \`$1'" >&2
21                 exit 0
22                 ;;
23 esac
24
25
26 # Include CARNet functions.
27 . /usr/share/carnet-tools/functions.sh
28
29 PKG="apache2-cn"
30 VERSION="2.2-1"
31 CONFDIR="/etc/apache2"
32 CONFDIROLD="/etc/apache"
33 CONF="$CONFDIR/apache2.conf"
34 CONFOLD="$CONFDIROLD/httpd.conf"
35 A2MODEDIR="$CONFDIR/mods-enabled"
36 PORTCONF="$CONFDIR/ports.conf"
37 A2CNDIR=/usr/share/apache2-cn
38 TMPLDIR=$A2CNDIR/templates
39 CERTDIR=/etc/ssl/certs
40 A2PHPINI="/etc/php4/apache2/php.ini"
41
42 HOST=$(hostname)
43 FQDN=$(hostname --fqdn)
44 WEBMASTER="webmaster@$FQDN"
45 DOMAIN=$(hostname -d)
46 BACKUPDIR="/var/backups/apache2-cn"
47
48 backup_done=0
49 need_restart=0
50 apache2_sslcert=0
51 apache2_sslcf=
52 apache2_sslckf=
53 apache2_sslccf=
54 has_vhosts=0
55 temp_files=
56 has_listen_ssl=0
57 listen_ssl_mask=
58
59
60 # cleanup()
61 #
62 #   Cleanup all temp files.
63 #
64 cleanup () {
65
66         if [ -n "$temp_files" ]; then
67                 for item in $temp_files; do
68                         if [ -e "$item" ]; then
69                                 rm -f $item
70                         fi
71                 done
72         fi
73 }
74
75 # tag_conf()
76 #
77 #   Add CARNet package info lines to config's header.
78 #
79 tag_conf () {
80         
81         local conf_file
82         conf_file="$1"
83         
84         if [ -e "$conf_file" ]; then
85         
86                 cat >> $conf_file <<EOF
87 ## Begin - Generated by CARNet package apache2-cn
88 #
89 #  REMOVE this whole block if you DON'T WANT apache2-cn
90 #  to edit your configuration file.
91 #
92 ## End - Generated by CARNet package apache2-cn
93 EOF
94         fi
95 }
96
97 # chk_conf_tag ()
98 #
99 #   Check if configuration file has CARNet package info lines.
100 #   return:  $RET => 0 - tagged
101 #                    1 - not tagged or file does not exists
102 #                    2 - file exists, but it is not tagged
103 #
104 chk_conf_tag () {
105
106         local conf_file
107         conf_file="$1"
108         RET=1
109         
110         if [ -f "$conf_file" ]; then
111                 if egrep -q "^## Begin - Generated by CARNet package apache2-cn$" "$conf_file"; then
112                         RET=0
113                 else
114                         RET=2
115                 fi
116         fi
117 }
118
119 # conf_log_fix ()
120 #
121 #   Check CustomLog, ErrorLog and TransferLog paths - /var/log/apache/ is replaced
122 #   with /var/log/apache2/.
123 #
124 conf_log_fix () {
125
126         local conf_file out
127         conf_file="$1"
128
129         if [ -f "$conf_file" ]; then
130         
131             if egrep -iq '^[[:space:]]*(Error|Custom|Transfer)Log[[:space:]]*\/var\/log\/apache\/' "$conf_file"; then
132             
133                 out=$(mktemp ${conf_file}.XXXXXX)
134                 temp_files="${temp_files} ${out}"
135             
136                 sed 's/\(^[[:space:]]*\(Error\|Custom\|Transfer\)Log[[:space:]]*\)\/var\/log\/apache\//\1\/var\/log\/apache2\//I' \
137                     $conf_file > $out
138                 mv $out $conf_file
139             fi
140         fi
141 }
142
143 # generate_ssl()
144 #
145 #   Generate Apache2 web server SSL certificate.
146 #
147 generate_ssl () {
148
149         generate_ssl_output=$($A2CNDIR/carnet-generate-ssl ignore "$FQDN" "$WEBMASTER" "$DOMAIN" 2> /dev/null)
150         cp_echo "$generate_ssl_output"
151         need_restart=1
152 }
153
154 # listen_ssl()
155 #
156 #   Check if port 443 is configured in ports.conf file.
157 #
158 listen_ssl() {
159         
160         if ! egrep -iq "^[[:space:]]*Listen[[:space:]]*.*443$" "$PORTCONF"; then
161         
162                 cp_echo "CN: Enabling SSL port (443) for Apache2 web server."
163
164                 out=$(mktemp ${PORTCONF}.XXXXXX)
165                 cp $PORTCONF $out
166                 echo "Listen 443" >> $out
167                 cp_mv $out $PORTCONF
168                 
169                 need_restart=1
170                 temp_files="${temp_files} ${out}"
171         fi
172 }
173
174 # install_conf()
175 #
176 #   Install specified Apache2 configuration file.
177 #
178 install_conf() {
179
180         conftmpl="$A2CNDIR/$1.conf"
181         conf="$CONFDIR/conf.d/$2.conf"
182
183         if [ ! -e "$conf" ]; then
184         
185                 cp_echo "CN: Enabling CARNet specific configuration."
186                 cp "$conftmpl" "$conf"
187                 
188                 need_restart=1
189         else
190                 cp_echo "CN: $conf already exists, left untouched." 1>&2
191         fi
192 }
193
194 # install_vhost()
195 #
196 #   Install specified VirtualHost for Apache2 web server.
197 #
198 #   Invocation:
199 #
200 #   install_vhost [-nvh] [-d] [-s docroot_symlink_dest] template site site-enabled-symlink
201 #
202 #     -nvh - add NameVirtualHost
203 #     -d   - mkdir DocumentRoot
204 #     -r   - set DocumentRoot
205 #     -n   - set ServerName
206 #     -s X - symlink DocumentRoot to X (all in /var/www)
207 #
208 #   site - name of file in sites-available, host part of ServerName unless -r or -n is used
209 #   site-enabled-symlink - name of symlink in sites-enabled
210 #
211 install_vhost() {
212
213         add_namevirthost=
214         mkdir_docroot=
215         symlink_docroot=
216         docroot=
217         vhostname=
218   
219         while echo "x$1" | grep -q '^x-'; do
220             case "$1" in
221                 -nvh)
222                     add_namevirthost=1
223                     shift
224                     ;;
225                 -d)
226                     mkdir_docroot=1
227                     shift
228                     ;;
229                 -s)
230                     shift
231                     symlink_docroot="$1"
232                     shift
233                     ;;
234                 -r)
235                     shift
236                     docroot="$1"
237                     if ! echo "$docroot" | grep -q /; then
238                             docroot="/var/www/$docroot"
239                     fi
240                     shift
241                     ;;
242                 -n)
243                     shift
244                     vhostname="$1"
245                     shift
246                     ;;
247             esac
248         done
249
250         vhosttmpl="$1.template"
251         vhost="$2"
252         venabled="$3"
253         [ -z "$vhostname" ] && vhostname=$(echo "$vhost"| awk -F. '{print $1}')
254         force_vhost=
255
256         vhostdir=$CONFDIR/sites-available
257         venabledir=$CONFDIR/sites-enabled
258
259         if [ ! -e "$TMPLDIR/${vhosttmpl}" ]; then
260                 echo "E: vhost template ${vhosttmpl} not found in $TMPLDIR!" 1>&2
261                 exit 2
262         fi
263
264         [ -z "$docroot" ] && docroot="/var/www/$vhostname.$DOMAIN"
265   
266         # if we were broken mid-installation, force
267         if [ ! -e "$docroot" -a \( -n "$mkdir_docroot" -o -n "$symlink_docroot" \) ]; then
268                 force_vhost=1
269         fi
270   
271         # add vhost if either of these is true
272         # - adding is forced OR
273         # - it doesn't exist
274         #
275         if [ -n "$force_vhost" -o \( ! -e "$vhostdir/$vhost" -a ! -e "$venabledir/$venabled" \) ]; then
276         
277                 cp_echo "CN: Adding $vhost VirtualHost."
278                 out=$(mktemp $vhostdir/$vhost.XXXXXX)
279                 temp_files="${temp_files} ${out}"
280                 
281                 # CARNet header.
282                 tag_conf "$out"
283
284                 if [ "$add_namevirthost" ]; then
285                         nvh=$(awk -F'[ >]' '/^<VirtualHost/ {print $2}' $TMPLDIR/$vhosttmpl |\
286                             sed "s/IPADDR/$MYIP/g")
287                         echo "NameVirtualHost $nvh" >> $out
288                 fi
289     
290                 sed "s/HOST/$vhostname/g; s/DOMAIN/$DOMAIN/g;
291                      s#DOCROOT#$docroot#g; s/IPADDR/$MYIP/g" < $TMPLDIR/$vhosttmpl >> $out
292                 cp_mv $out $vhostdir/$vhost
293                 chmod 644 $vhostdir/$vhost
294                 ln -fs ../sites-available/$vhost $venabledir/$venabled    
295     
296                 if [ -n "$mkdir_docroot" -a ! -d "$docroot" ]; then
297                         mkdir "$docroot"
298                         echo '<html><body><h1>Radi!</h1></body></html>' > "$docroot/index.html"
299                 elif [ -n "$symlink_docroot" ]; then
300                         ln -fs "$symlink_docroot" "$docroot"
301                 fi
302
303                 need_restart=1
304         fi
305 }
306
307
308 # Set trap for deleting all temp files.
309 #
310 trap cleanup 0 1 2 15
311
312
313 # Make sure that monit conf for Apache is disabled.
314 if [ -f "/etc/monit.d/apache1.conf" ]; then
315         mv /etc/monit.d/apache1.conf /etc/monit.d/apache1.conf.disabled
316         pkill -9 -f /usr/sbin/monit || true
317 fi
318
319
320 # First of all - stop Apache web server, make sure Apache is NOT running.
321 #
322 if [ -x /usr/sbin/invoke-rc.d ]; then
323         [ -x /usr/sbin/apache ] && invoke-rc.d apache stop || true
324         pkill -9 -f /usr/sbin/apache || true
325 else
326         [ -x /etc/init.d/apache ] && /etc/init.d/apache stop || true
327 fi
328
329
330 # Backup all configuration located in /etc/apache2/conf.d/ and
331 # /etc/apache2/sites-available/ directories.
332 #
333 if [ -e "$CONF" ]; then
334         cp_echo "CN: Doing backup for $CONF"
335         cp_backup_conffile -d $BACKUPDIR -p $CONF
336         backup_done=1
337 fi
338 if [ -d "$CONFDIR/conf.d" ] && [ -n "$(ls ${CONFDIR}/conf.d/)" ]; then
339         cp_echo "CN: Doing backup for all files in /etc/apache2/conf.d/"
340         for file in /etc/apache2/conf.d/*; do
341             if [ -z "$(echo $file | egrep '^/.*~')" ]; then
342                 cp_backup_conffile -d $BACKUPDIR -p $file
343             fi
344         done
345         backup_done=1
346 fi
347 if [ -d "$CONFDIR/sites-available" ] && [ -n "$(ls ${CONFDIR}/sites-available/)" ]; then
348         cp_echo "CN: Doing backup for all files in /etc/apache2/sites-available/"
349         for file in /etc/apache2/sites-available/*; do
350             if [ -z "$(echo $file | egrep '^/.*~')" ]; then
351                 cp_backup_conffile -d $BACKUPDIR -p $file
352             fi
353         done
354         backup_done=1
355 fi
356 if [ $backup_done -eq 1 ]; then
357         cp_echo "CN: Backup is located in directory: $BACKUPDIR/"
358 fi
359
360
361 # Enable Apache2 web server modules (cgi, rewrite, userdir, suexec, php4, ssl).
362 #
363 if [ -e "$CONF" ]; then
364
365         if [ ! -e "$A2MODEDIR/cgi.load" ]; then
366                 cp_echo "CN: Enabling CGI module for Apache2 web server."
367                 a2enmod cgi >/dev/null || true
368                 need_restart=1
369         fi
370
371         if [ ! -e "$A2MODEDIR/rewrite.load" ]; then
372                 cp_echo "CN: Enabling rewrite module for Apache2 web server."
373                 a2enmod rewrite >/dev/null || true
374                 need_restart=1
375         fi
376
377         if [ ! -e "$A2MODEDIR/userdir.load" ] || [ ! -e "$A2MODEDIR/userdir.conf" ]; then
378                 cp_echo "CN: Enabling userdir module for Apache2 web server."
379                 a2enmod userdir >/dev/null || true
380                 need_restart=1
381         fi
382
383         if [ ! -e "$A2MODEDIR/suexec.load" ]; then
384                 cp_echo "CN: Enabling SUEXEC module for Apache2 web server."
385                 a2enmod suexec >/dev/null || true
386                 need_restart=1
387         fi
388
389         if [ ! -e "$A2MODEDIR/php4.load" ] || [ ! -e "$A2MODEDIR/php4.conf" ]; then
390                 cp_echo "CN: Enabling PHP4 module for Apache2 web server."
391                 a2enmod php4 >/dev/null || true
392                 need_restart=1
393         fi
394
395         if [ ! -e "$A2MODEDIR/ssl.load" ] || [ ! -e "$A2MODEDIR/ssl.conf" ]; then
396                 cp_echo "CN: Enabling SSL module for Apache2 web server."
397                 a2enmod ssl >/dev/null || true
398                 need_restart=1
399         fi
400 fi
401
402
403 # Install CARNet specific configuration file.
404 #
405 install_conf carnet 000-carnet
406
407 # Enable SSL port (443).
408 #
409 listen_ssl
410
411 # Disable default site configuration.
412 #
413 if [ -e "$CONF" ]; then
414         if [ -e "$CONFDIR/sites-enabled/000-default" ]; then
415                 cp_echo "CN: Disabling 000-default site configuration."
416                 a2dissite 000-default >/dev/null || true
417
418                 need_restart=1
419         fi
420 fi
421
422
423 # Apache2 SSL certificate.
424 #
425 has_listen_ssl=0
426
427 if [ -d "$CONFDIR/conf.d" ] && [ -n "$(ls $CONFDIR/conf.d)" ]; then
428         listen_ssl_mask=$CONFDIR/conf.d/*
429 fi
430 if [ -d "$CONFDIR/sites-enabled" ] && [ -n "$(ls $CONFDIR/sites-enabled)" ]; then
431         listen_ssl_mask=$listen_ssl_mask" "$CONFDIR/sites-enabled/*
432 fi
433
434 for file in $CONF $listen_ssl_mask; do
435         if [ -f "$file" ]; then
436                 if egrep -iq '^[[:space:]]*<VirtualHost .*443[[:space:]]*>' $file; then
437                         has_listen_ssl=1
438                         apache2_sslcert=1
439                         break
440                 fi
441         fi
442 done
443
444 if [ $apache2_sslcert -eq 0 ]; then
445
446         db_get apache2-cn/sslcf || true
447         apache2_sslcf="$RET"
448
449         if [ -n "$apache2_sslcf" ]; then
450
451                 db_get apache2-cn/sslckf || true
452                 apache2_sslckf="$RET"
453
454                 db_get apache2-cn/sslccf || true
455                 apache2_sslccf="$RET"
456         
457                 need_restart=1
458         else
459
460                 # Generate new SSL certificate files.
461                 generate_ssl
462         
463                 apache2_sslcf=
464                 apache2_sslckf=
465                 apache2_sslccf=
466         fi
467 fi
468
469
470 # Add VirtualHosts.
471 #
472 db_get apache2-cn/wwwhost || true
473 if [ "$RET" = "true" ]; then
474
475         # Add WWW VirtualHost.
476         if [ -f "$CONFDIR/sites-available/$FQDN" ]; then
477                 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/$FQDN
478         fi
479         if [ -f "$CONFDIR/sites-available/www.$DOMAIN" ]; then
480                 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/www.$DOMAIN
481         fi
482
483         chk_conf_tag "$CONFDIR/sites-available/$FQDN"
484         if [ ! -f "$CONFDIR/sites-available/$FQDN" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
485                 install_vhost -nvh -d -r www.$DOMAIN default $FQDN 000-$FQDN
486                 need_restart=1
487         fi
488
489         chk_conf_tag "$CONFDIR/sites-available/www.$DOMAIN"
490         if [ ! -f "$CONFDIR/sites-available/www.$DOMAIN" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
491                 install_vhost default www.$DOMAIN www.$DOMAIN
492                 need_restart=1
493         fi
494 else
495
496         # No WWW VirtualHost.
497         if [ -f "$CONFDIR/sites-available/$FQDN" ]; then
498                 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/$FQDN
499         fi
500
501         chk_conf_tag "$CONFDIR/sites-available/$FQDN"
502         if [ ! -f "$CONFDIR/sites-available/$FQDN" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
503                 install_vhost -nvh -d -r $FQDN default $FQDN 000-$FQDN
504                 need_restart=1
505         fi
506 fi
507
508
509 # Add VirtualHost for SSL?
510 #
511 if [ $apache2_sslcert -eq 0 ]; then
512
513         if [ -f "$CONFDIR/sites-available/ssl" ]; then
514                 cp_backup_conffile -d $BACKUPDIR -p $CONFDIR/sites-available/ssl
515         fi
516
517         # No active SSL VirtualHosts found - add new one.
518         chk_conf_tag "$CONFDIR/sites-available/ssl"
519         if [ ! -f "$CONFDIR/sites-available/ssl" ] || [ $RET -eq 0 -a -f "$CONFOLD" ]; then
520                 install_vhost -r $FQDN -n $HOST ssl ssl 001-ssl
521                 need_restart=1
522         fi
523 fi
524
525
526 # Check SSL certificates location for VirtualHosts.
527 #
528 if [ $apache2_sslcert -eq 0 ]; then
529
530         chk_conf_tag "${CONFDIR}/sites-available/ssl"
531         if [ $RET -eq 0 ] && [ -n "$apache2_sslcf" ]; then
532
533                 SSLTMP=$(mktemp ${CONFDIR}/ssltmp.XXXXXX)
534                 temp_files="${temp_files} ${SSLTMP}"
535                 cp ${CONFDIR}/sites-available/ssl $SSLTMP
536
537                 # SSLCertificateFile
538                 cp_check_and_sed "^[[:space:]]*SSLCertificateFile \/etc\/ssl\/certs\/apache2\.pem" \
539                     "s#SSLCertificateFile /etc/ssl/certs/apache2.pem#SSLCertificateFile $apache2_sslcf #g" \
540                     $SSLTMP || true
541
542                 # SSLCertificateKeyFile
543                 cp_check_and_sed "^[[:space:]]*SSLCertificateKeyFile \/etc\/ssl\/private\/apache2\.key" \
544                     "s#SSLCertificateKeyFile /etc/ssl/private/apache2.key#SSLCertificateKeyFile $apache2_sslckf #g" \
545                     $SSLTMP || true
546
547                 # SSLCertificateChainFile
548                 if [ -n "$apache2_sslccf" ]; then
549                 cp_check_and_sed "^# SSLCertificateChainFile \/etc\/ssl\/certs/sureserverEDU\.pem" \
550                     "s#\# SSLCertificateChainFile /etc/ssl/certs/sureserverEDU.pem#SSLCertificateChainFile $apache2_sslccf #g" \
551                     $SSLTMP || true
552                 fi
553
554                 cp_mv $SSLTMP ${CONFDIR}/sites-available/ssl
555
556                 need_restart=1
557
558                 # Just to be sure.
559                 if [ -e "$SSLTMP" ]; then
560                         rm -f $SSLTMP
561                 fi
562         fi
563 fi
564
565
566 # Check for CustomLog, ErrorLog and TransferLog in Apache2 configuration.
567 #
568 cp_echo "CN: Checking Apache2 CustomLog, ErrorLog and TransferLog directives."
569 if [ -d "$CONFDIR/conf.d" ] && [ -n "$(ls $CONFDIR/conf.d)" ]; then
570         log_mask=$CONFDIR/conf.d/*
571 fi
572 if [ -d "$CONFDIR/sites-available" ] && [ -n "$(ls $CONFDIR/sites-available)" ]; then
573         log_mask=$log_mask" "$CONFDIR/sites-available/*
574 fi
575 for file in $CONF $log_mask; do
576         chk_conf_tag "$file"
577         if [ $RET -eq 0 ]; then
578                 conf_log_fix "$file"
579         fi
580 done
581
582
583 # Start Apache2 web server on boot?
584 # This will enable Apache2 in /etc/default/apache2 file.
585 #
586 if egrep -q "^[[:space:]]*NO_START=1" /etc/default/apache2; then
587         cp_check_and_sed NO_START=1 s/NO_START=1/NO_START=0/ /etc/default/apache2 || true
588         need_restart=1
589 fi
590
591
592 db_stop || true
593
594
595 # Remove old AOSI configuration for Apache: aosi-www.conf, aosi.conf.
596 #
597 if [ -e "$CONFDIR/conf.d/aosi-www.conf" ] || [ -e "$CONFDIR/conf.d/aosi.conf" ]; then
598         cp_echo "CN: Removing old AOSI configuration files for Apache2."
599         need_restart=1
600 fi
601 [ -e "$CONFDIR/conf.d/aosi-www.conf" ] && rm -f $CONFDIR/conf.d/aosi-www.conf
602 [ -e "$CONFDIR/conf.d/aosi.conf" ] && rm -f $CONFDIR/conf.d/aosi.conf
603
604
605 # Stop Apache web server and disable Apache automatic start on boot.
606 #
607 if [ -x "/etc/init.d/apache" ]; then
608
609         # Stop Apache.
610         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
611                 invoke-rc.d apache stop || true
612         else
613                 /etc/init.d/apache stop || true
614         fi
615
616         # Disable automatic start on boot.
617         if [ -x "`which update-rc.d 2>/dev/null`" ]; then
618                 update-rc.d -f apache remove > /dev/null 2>&1 || true
619                 update-rc.d apache stop 90 6 . > /dev/null 2>&1 || true
620         fi
621 fi
622
623 # Also check for Apache-SSL web server.
624 #
625 if [ -x "/etc/init.d/apache-ssl" ]; then
626
627         # Stop Apache-SSL.
628         if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
629                 invoke-rc.d apache-ssl stop || true
630         else
631                 /etc/init.d/apache-ssl stop || true
632         fi
633
634         # Disable automatic start on boot.
635         if [ -x "`which update-rc.d 2>/dev/null`" ]; then
636                 update-rc.d -f apache-ssl remove > /dev/null 2>&1 || true
637                 update-rc.d apache-ssl stop 90 6 . > /dev/null 2>&1 || true
638         fi
639 fi
640
641
642 # Restart Apache2 web server if needed.
643 #
644 if [ $need_restart -eq 1 ]; then
645
646         # Check Apache2 web server configuration.
647         if apache2ctl configtest 2>/dev/null; then
648
649                 # Restart Apache2 web server.
650                 if [ -x "/etc/init.d/apache2" ]; then
651                     if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
652                         invoke-rc.d apache2 force-reload || true
653                     else
654                         /etc/init.d/apache2 force-reload || true
655                     fi
656                 fi
657         else
658
659                 # Something is broken.
660                 cp_echo "CN: Your Apache2 configuration seem to be broken."
661                 cp_echo "CN: Please, check the service after the installation finishes!"
662         fi
663 fi
664
665
666 # Mail root
667 #
668 cp_mail "$PKG"
669
670
671 # (re)generate monit.d files if monit-cn is installed.
672 #
673 if [ -x "/usr/sbin/update-monit.d" ]; then
674         update-monit.d || true
675 fi
676
677
678 exit 0