knows about apache 2.4 new access scheme
[myplc.git] / plc.d / functions
1 # -*-Shell-script-*-
2 # Common functions for PLC startup/shutdown scripts
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 export PATH=/sbin:/bin:/usr/bin:/usr/sbin
9
10 # Source function library
11 . /etc/init.d/functions
12
13 # Total number of errors
14 ERRORS=0
15
16 # Count the exit status of the last command
17 function check () {
18     ERRORS=$(($ERRORS+$?))
19 }
20
21 # Print status header
22 function dialog () {
23     echo -n "PLC: $*: " >&3
24 }
25
26 # Print result
27 function result () {
28     if [ $ERRORS -eq 0 ] ; then
29         success "$*" >&3
30     else
31         failure "$*" >&3
32     fi
33     echo >&3
34 }
35
36 # Start up a program with a plc_ prefix
37 function plc_daemon () {
38     base=${1##*/}
39
40     # See if it's already running. Look *only* at the pid file.
41     if [ -f /var/run/plc_${base}.pid ]; then
42         local line p
43         read line < /var/run/plc_${base}.pid
44         for p in $line ; do
45                 [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
46         done
47     fi
48
49     [ -n "${pid:-}" -a -z "${force:-}" ] && return
50
51     # And start it up.
52     # Thierry -- June 18 2007
53     # when invoking, e.g. service plc start httpd from an ssh connection
54     # ssh stupidly hangs when everything is done
55     # it turns out the forked ssh daemon exhibits the following stack at that point
56     # (gdb) where
57     # #0  0x001d6402 in __kernel_vsyscall ()
58     # #1  0x003c2e7d in ___newselect_nocancel () from /lib/libc.so.6
59     # #2  0x004387b4 in main () from /usr/sbin/sshd
60     # So I figured the various file descriptors used were not properly closed
61     (exec 3>&- 4>&- ; exec -a plc_${base} $*)
62     ret=$?
63
64     if [ -f /var/run/${base}.pid ] ; then
65         mv /var/run/${base}.pid /var/run/plc_${base}.pid
66     fi
67
68     return $ret
69 }
70
71 # Print IP address of hostname if resolvable
72 function gethostbyname () {
73     python -c 'import socket; import sys; print socket.gethostbyname(sys.argv[1])' $1 2>/dev/null
74 }
75
76 # Forcefully make a symlink
77 function symlink () {
78     mkdir -p $(dirname $2)
79     rm -f $2
80     ln -s $1 $2
81 }
82
83 # Argument(s) or stdin to lowercase stdout
84 function lower () {
85     if [ ${#*} -ge 1 ] ; then
86         tr A-Z a-z <<<$*
87     else
88         tr A-Z a-z
89     fi
90 }
91
92 # Argument(s) or stdin to uppercase stdout
93 function upper () {
94     if [ ${#*} -ge 1 ] ; then
95         tr a-z A-Z <<<$*
96     else
97         tr a-z A-Z
98     fi
99 }
100
101 # Regenerate configuration files
102 function plc_reload () {
103     force=$1
104
105     # Regenerate the main configuration file from default values
106     # overlaid with site-specific and current values.
107     # Thierry -- 2007-07-05 : values in plc_config.xml are *not* taken into account here
108     files=(
109         /etc/planetlab/default_config.xml
110         /etc/planetlab/configs/site.xml
111     )
112
113     for file in "${files[@]}" ; do
114     if [ -n "$force" -o $file -nt /etc/planetlab/plc_config.xml ] ; then
115         tmp=$(mktemp /tmp/plc_config.xml.XXXXXX)
116         plc-config --xml "${files[@]}" >$tmp
117         if [ $? -eq 0 ] ; then
118         mv $tmp /etc/planetlab/plc_config.xml
119         chmod 444 /etc/planetlab/plc_config.xml
120         else
121         echo "PLC: Warning: Invalid configuration file(s) detected"
122         rm -f $tmp
123         fi
124         break
125     fi
126     done
127
128     # Convert configuration to various formats
129     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config ] ; then
130     plc-config --shell >/etc/planetlab/plc_config
131     fi
132     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/plc_config.py ] ; then
133     plc-config --python >/etc/planetlab/plc_config.py
134     fi
135     if [ -n "$force" -o /etc/planetlab/plc_config.xml -nt /etc/planetlab/php/plc_config.php ] ; then
136     mkdir -p /etc/planetlab/php
137     plc-config --php >/etc/planetlab/php/plc_config.php
138     fi
139 }
140
141 #################### feb 2013 for f18
142 # Authorization directives change in apache 2.4
143 # http://httpd.apache.org/docs/2.4/upgrading.html#access
144 #### up to 2.2, this would be e.g.
145 # apachectl -V
146 # Server version: Apache/2.2.22 (Unix)
147 # <Directory "/usr/share/plc_api/apache">
148 # Options       +ExecCGI
149 # Order allow,deny
150 # Allow from all
151 # </Directory>
152 #### starting with 2.4 it becomes
153 # apachectl -V
154 # Server version: Apache/2.4.3 (Fedora)
155 # <Directory "/usr/share/plc_api/apache">
156 # Options       +ExecCGI
157 # Require       all granted
158 # </Directory>
159
160 function apache_newauth () {
161     apache_version=$(apachectl -V 2> /dev/null | grep 'Server version' | cut -d ' ' -f3 | sed -e 's,^.*/,,')
162     apache_major=$(echo $apache_version | cut -d. -f1)
163     apache_minor=$(echo $apache_version | cut -d. -f2)
164     test "$apache_minor" -ge 4
165 }
166 function apache_allow () {
167     if apache_newauth; then
168         echo -e "Require all granted"
169     else
170         echo -e "Order allow,deny\n   Allow from all"
171     fi
172 }
173 function apache_forbid () {
174     if apache_newauth; then
175         echo -e "Require all denied"
176     else
177         echo -e "Order deny,allow\n   Deny from all"
178     fi
179 }
180
181 # Make copies of stdout and stderr. The plc initscript redirects
182 # stdout and stderr to a logfile if -v is not specified.
183 [ ! -e /proc/self/fd/3 ] && exec 3>&1
184 [ ! -e /proc/self/fd/4 ] && exec 4>&2