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