- don't source shell configuration in /etc/plc.d/functions, which is
[myplc.git] / plc.d / httpd
1 #!/bin/bash
2 #
3 # priority: 700
4 #
5 # Configure Apache web server
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id: httpd,v 1.1 2006/04/06 21:51:59 mlhuang Exp $
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Default locations
18 DocumentRoot=/var/www/html
19 php_ini=/etc/php.ini
20 httpd_conf=/etc/httpd/conf/httpd.conf
21 ssl_conf=/etc/httpd/conf.d/ssl.conf
22 plc_conf=/etc/httpd/conf.d/plc.conf
23
24 case "$1" in
25     start)
26         if [ "$PLC_API_ENABLED" != "1" -a \
27              "$PLC_BOOT_ENABLED" != "1" -a \
28              "$PLC_WWW_ENABLED" != "1" ] ; then
29             exit 0
30         fi
31
32         MESSAGE=$"Starting web server"
33         dialog "$MESSAGE"
34
35         # Set the default include path
36         include_path=".:$DocumentRoot/includes:$DocumentRoot/generated:/etc/planetlab/php"
37         sed -i -e "s@;include_path = \"\.:.*\"@include_path = \"$include_path\"@" $php_ini
38
39         # Disable default Listen directive
40         sed -i -e '/^Listen/d' $httpd_conf
41
42         # Set the port numbers
43         for server in WWW API BOOT ; do
44             enabled=PLC_${server}_ENABLED
45             if [ "${!enabled}" != "1" ] ; then
46                 continue
47             fi
48             hostname=PLC_${server}_HOST
49             http_port=PLC_${server}_PORT
50             https_port=PLC_${server}_SSL_PORT
51
52             # API should always be accessed via SSL
53             if [ "$server" = "API" ] ; then
54                 https_port=${!http_port}
55                 http_port=
56             fi
57
58             # Check if we are already listening on these ports
59             skip_http=0
60             skip_https=0
61             for previous_server in WWW API BOOT ; do
62                 if [ "$server" = "$previous_server" ] ; then
63                     break
64                 fi
65                 previous_hostname=PLC_${previous_server}_HOST
66                 previous_http_port=PLC_${previous_server}_PORT
67                 previous_https_port=PLC_${previous_server}_SSL_PORT
68
69                 if [ "${!http_port}" = "${!previous_http_port}" ] ; then
70                     skip_http=1
71                 fi
72                 if [ "${!https_port}" = "${!previous_https_port}" ] ; then
73                     skip_https=1
74                 fi
75             done
76
77             # Listen on these ports
78             if [ $skip_http -eq 0 -a -n "${!http_port}" ] ; then
79                 cat <<EOF
80 Listen ${!http_port}
81 <VirtualHost *:${!http_port}>
82     Redirect /db https://$PLC_WWW_HOST:$PLC_WWW_SSL_PORT/db
83     # XXX Not yet until we can get rid of oldapi
84     # Redirect /$PLC_API_PATH https://$PLC_API_HOST:$PLC_API_PORT/$PLC_API_PATH
85 </VirtualHost>
86 EOF
87             fi
88             if [ $skip_https -eq 0 -a -n "${!https_port}" ] ; then
89                 # XXX Cannot support NameVirtualHost over SSL. If
90                 # the API, boot, and web servers are all running
91                 # on the same machine, the web server certificate
92                 # takes precedence.
93                 sed -i \
94                     -e "s/^Listen .*/Listen ${!https_port}/" \
95                     -e "s/<VirtualHost _default_:.*>/<VirtualHost _default_:${!https_port}>/" \
96                     $ssl_conf
97             fi
98         done >$plc_conf
99
100         # Set custom Apache directives
101         (
102             if [ "$PLC_API_ENABLED" = "1" ] ; then
103                 cat <<EOF
104 <Location $PLC_API_PATH>
105     SetHandler python-program
106     PythonPath "sys.path + ['/usr/share/plc_api']"
107     PythonHandler mod_pythonXMLRPC
108 </Location>
109 EOF
110             else
111                 cat <<EOF
112 <Location $PLC_API_PATH>
113     Deny from all
114 </Location>
115 EOF
116             fi
117
118             if [ "$PLC_WWW_ENABLED" != "1" ] ; then
119                 cat <<EOF
120 <Location /db>
121     Deny from all
122 </Location>
123 EOF
124             fi
125         ) >>$plc_conf
126
127         # Make alpina-logs directory writable for bootmanager log upload
128         chown apache:apache $DocumentRoot/alpina-logs/nodes
129
130         plc_daemon httpd
131         check
132
133         result "$MESSAGE"
134         ;;
135
136     stop)
137         MESSAGE=$"Stopping web server"
138         dialog "$MESSAGE"
139
140         killproc plc_httpd
141         check
142
143         result "$MESSAGE"
144         ;;
145 esac
146
147 exit $ERRORS