Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / redhat / init
1 #!/bin/bash
2 #
3 # ntpd          This shell script takes care of starting and stopping
4 #               trust_router.
5 #
6 # chkconfig: - 58 74
7 # description: trust_router is the GSS-EAP trust routing daemon. \
8 # GSS-EAP is an IETF standard for providing authentication across \
9 # an insecure WAN. \
10
11 ### BEGIN INIT INFO
12 # Provides: trust_router
13 # Required-Start: $network $local_fs $remote_fs
14 # Required-Stop: $network $local_fs $remote_fs
15 # Should-Start: $syslog $named ntpdate
16 # Should-Stop: $syslog $named
17 # Short-Description: start and stop trust_router
18 # Description: trust_router is the GSS-EAP trust routing daemon.
19 #              GSS-EAP is an IETF standard for providing authentication
20 #              across an insecure WAN.
21 ### END INIT INFO
22
23 # Source function library.
24 . /etc/init.d/functions
25
26 # Source networking configuration.
27 . /etc/sysconfig/network
28
29 # Load the instance configuration
30 [ -f /etc/sysconfig/trust_router ] || exit 6
31 . /etc/sysconfig/trust_router
32
33 # Create the pidfile directory
34 mkdir -p /var/run/trust_router
35 chown trustrouter:trustrouter /var/run/trust_router
36
37 # Does the trust router and wrapper exist
38 [ -x /usr/bin/trust_router ] || exit 5
39 [ -x /usr/bin/trust_router-wrapper ] || exit 5
40
41 # Does the tidc client exist
42 [ -x /usr/bin/tidc ] || exit 5
43 [ -x /usr/bin/tidc-wrapper ] || exit 5
44
45 prog="trust_router-wrapper"
46
47 array_contains() {
48         local i
49
50         for i in "${@:2}" ;
51         do
52                 [ "${i}" == "${1}" ] && return 0
53         done
54
55         return 1
56 }
57
58 execute-tidc() {
59         echo -n "Attempting to authenticate to instance ${current_instance}:${current_port}"
60
61         daemon --user="${current_user}" /usr/bin/tidc-wrapper "${current_test_acceptor}" "${current_test_rprealm}" "${current_test_community}" "${current_test_realm}" "${current_port}"
62         tidc_ret=$?
63
64         echo
65
66         return ${tidc_ret}
67 }
68
69 get-config() {
70         current_instance=${1}
71
72         if [ "${TR_CONFIG_USER[${current_instance}]+abc}" ] ;
73         then
74                 current_user=${TR_CONFIG_USER[${current_instance}]}
75         else
76                 current_user=${TR_DEFAULT_USER}
77         fi
78
79         if [ "${TR_CONFIG_PIDDIR[${current_instance}]+abc}" ] ;
80         then
81                 current_piddir=${TR_CONFIG_PIDDIR[${current_instance}]}
82         else
83                 current_piddir=${TR_DEFAULT_PIDDIR}
84         fi
85
86         if [ "${TR_CONFIG_CFGDIR[${current_instance}]+abc}" ] ;
87         then
88                 current_cfgdir=${TR_CONFIG_CFGDIR[${current_instance}]}
89         else
90                 current_cfgdir=${TR_DEFAULT_CFGDIR}
91         fi
92
93         if [ "${TR_CONFIG_LOGDIR[${current_instance}]+abc}" ] ;
94         then
95                 current_logdir=${TR_CONFIG_LOGDIR[${current_instance}]}
96         else
97                 current_logdir=${TR_DEFAULT_LOGDIR}
98         fi
99
100         if [ "${TR_CONFIG_PORT[${current_instance}]+abc}" ] ;
101         then
102                 current_port=${TR_CONFIG_PORT[${current_instance}]}
103         else
104                 current_port=${TR_DEFAULT_PORT}
105         fi
106
107         if [ "${TR_CONFIG_AUTOSTART[${current_instance}]+abc}" ] ;
108         then
109                 current_autostart=${TR_CONFIG_AUTOSTART[${current_instance}]}
110         else
111                 current_autostart=${TR_DEFAULT_AUTOSTART}
112         fi
113
114         if [ "${TR_CONFIG_ACCEPTOR[${current_instance}]+abc}" ] ;
115         then
116                 current_test_acceptor=${TR_CONFIG_TEST_ACCEPTOR[${current_instance}]}
117         else
118                 current_test_acceptor=${TR_DEFAULT_TEST_ACCEPTOR}
119         fi
120
121         if [ "${TR_CONFIG_RPREALM[${current_instance}]+abc}" ] ;
122         then
123                 current_test_rprealm=${TR_CONFIG_TEST_RPREALM[${current_instance}]}
124         else
125                 current_test_rprealm=${TR_DEFAULT_TEST_RPREALM}
126         fi
127
128         if [ "${TR_CONFIG_TEST_COMMUNITY[${current_instance}]+abc}" ] ;
129         then
130                 current_test_community=${TR_CONFIG_TEST_COMMUNITY[${current_instance}]}
131         else
132                 current_test_community=${TR_DEFAULT_TEST_COMMUNITY}
133         fi
134
135         if [ "${TR_CONFIG_TEST_REALM[${current_instance}]+abc}" ] ;
136         then
137                 current_test_realm=${TR_CONFIG_TEST_REALM[${current_instance}]}
138         else
139                 current_test_realm=${TR_DEFAULT_TEST_REALM}
140         fi
141 }
142
143 get-pidfile() {
144         echo "${current_piddir}/${current_instance}.pid"
145 }
146
147 start() {
148         [ "${EUID}" != "0" ] && exit 4
149         [ "${NETWORKING}" = "no" ] && exit 1
150
151         start_ret=0
152
153         for i in "${TR_INSTANCES[@]}"
154         do
155                 get-config "${i}"
156
157                 if ${current_autostart} ;
158                 then
159                         start-instance
160                         let "start_ret+=$?"
161                 else
162                         echo "Skipping instance ${current_instance}"
163                 fi
164         done
165
166         return "${start_ret}"
167 }
168
169 start-single() {
170         if array_contains "${1}" "${TR_INSTANCES[@]}" ;
171         then
172                 get-config "${1}"
173
174                 start-instance
175                 return $?
176         else
177                 echo "Instance ${1} not found..."
178
179                 return 1
180         fi
181 }
182
183 start-instance() {
184         pidfile=$(get-pidfile)
185         logfile="${current_logdir}/${current_instance}.log"
186         cfgdir="${current_cfgdir}/${current_instance}/"
187
188         OPTIONS="${pidfile} ${cfgdir} ${logfile}"
189
190         if [ -f "${pidfile}" ] ;
191         then
192                 local OLD_PID=$(cat "${pidfile}")
193
194                 if [ -d "/proc/${OLD_PID}" ] ;
195                 then
196                         echo "Instance ${current_instance} is already running..."
197                 else
198                         echo "Removing stale PID file..."
199                         rm "${pidfile}"
200
201                         start-instance
202                         return $?
203                 fi
204         else
205                 echo -n "Starting instance ${current_instance}..."
206                 daemon --user="${current_user}" --pidfile="${pidfile}" "${prog}" "${OPTIONS}"
207                 echo
208         fi
209
210         # Give it a few seconds for things to settle
211         sleep 2
212
213         execute-tidc
214         return $?
215 }
216
217 stop() {
218         [ "${EUID}" != "0" ] && exit 4
219         [ "${NETWORKING}" = "no" ] && exit 1
220
221         for i in "${TR_INSTANCES[@]}"
222         do
223                 get-config "${i}"
224
225                 pidfile=$(get-pidfile)
226
227                 if [ -f "${pidfile}" ] ;
228                 then
229                         stop-instance
230                 else
231                         echo "Instance ${i} does not appear to be running..."
232                 fi
233         done
234 }
235
236 stop-single() {
237         if array_contains "${1}" "${TR_INSTANCES[@]}" ;
238         then
239                 get-config "${1}"
240
241                 stop-instance
242         else
243                 echo "Instance ${1} not found..."
244
245                 return 1
246         fi
247
248         return 0
249 }
250
251 stop-instance() {
252         if [ -f "$(get-pidfile)" ]  ;
253         then
254                 echo -n "Stopping instance ${current_instance}..."
255
256                 killproc -p "$(get-pidfile)" "${prog}"
257                 echo
258         else
259                 echo "Instance ${current_instance} does not appear to be running..."
260         fi
261 }
262
263 status() {
264         [ "${EUID}" != "0" ] && exit 4
265         [ "${NETWORKING}" = "no" ] && exit 1
266
267         start_ret=0
268
269         for i in "${TR_INSTANCES[@]}"
270         do
271                 get-config "${i}"
272
273                 execute-tidc
274                 let "exec_ret+=$?"
275         done
276
277         return "${exec_ret}"
278 }
279
280
281 # See how we were called.
282 case "${1}" in
283   start)
284         start
285         ;;
286   start-single)
287         start-single "${2}"
288         ;;
289   stop)
290         stop
291         ;;
292   stop-single)
293         stop-single "${2}"
294         ;;
295   status)
296         status "${prog}"
297         ;;
298   restart|force-reload)
299         stop
300         start
301         ;;
302   reload)
303         exit 3
304         ;;
305   *)
306         echo "Usage: ${0} {start|start-single|stop|stop-single|status|restart|force-reload}"
307         exit 2
308 esac