Support multiiso cds
[grml-terminalserver.git] / grml-terminalserver
1 #!/bin/bash
2 # Filename:      terminalserver
3 # Purpose:       Program to do something
4 # Authors:       grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Sat Aug 06 14:14:40 CEST 2005
8 ################################################################################
9
10
11 ###
12 ### __INCLUDES
13 ###
14 . /etc/grml/sh-lib
15 #. /etc/grml/sysexits-sh
16
17
18
19 ###
20 ### __VARIABLES
21 ###
22
23 FORCE_='false'
24 verbose_=0
25
26 # this file holds all variable definitions
27 SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
28 isExistent $SHARED_PROG_VARS_ die
29 . $SHARED_PROG_VARS_
30
31
32 ###
33 ### __FUNCTIONS
34 ###
35
36 function printUsage
37 {
38   cat <<EOT
39 Usage: "$PROG_NAME__" [OPTIONS] <command>
40
41 $PROG_NAME__ is the config program for the terminalserver
42 comming with grml.
43
44 COMMANDS:
45    help             This help text
46    start <service>  Start services (if all services are started, configs will be updated)
47    stop <service>   Stop services
48    config <service> Update config of given service (or from all if no services given)
49    clean            Stop all + remove config and boot files from services
50    <default>        interactive
51
52 SERVICES:
53   tftp        Tftp daemon
54   dhcp        Dhcp daemon
55   ipt         Iptables setup (snat for clients)
56   nfs         All necessary nfs daemons
57   <>          ALL services
58
59 OPTIONS:
60    -v         verbose (show what is going on, v++)
61    -h         this help text
62    -f         Force
63
64 EOT
65 }
66
67 function killPortmapper
68 {
69     /etc/init.d/portmap stop >/dev/null &>/dev/null
70     killall -9 portmap &>/dev/null
71 }
72
73 # DHCP SERVICE {{{
74 function createDhcpConf
75 {
76   if [ -e "$DHCPD_CONFIG_FILE_" ]; then
77     if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
78       execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
79       execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
80     else
81       if [[ $FORCE_ == "true" ]]; then
82         execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
83         execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
84       else
85         warn "Not updating user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
86       fi
87     fi
88   else
89     execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
90   fi
91 }
92
93 function removeDhcpConf
94 {
95   if [ -e "$DHCPD_CONFIG_FILE_" ]; then
96     if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
97       rm -f "$DHCPD_CONFIG_FILE_"
98     else
99       if [[ $FORCE_ == "true" ]]; then
100         rm -f "$DHCPD_CONFIG_FILE_"
101       else
102         warn "Not deleting user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
103       fi
104     fi
105   fi
106 }
107
108
109 function stopDhcp
110 {
111   start-stop-daemon --stop --quiet --pidfile "$DHCPD_PID_"
112   rm -f $DHCPD_PID_
113   rm -f /var/lib/dhcp3/dhcpd.leases* 2>/dev/null    #FIXME
114   touch /var/lib/dhcp3/dhcpd.leases
115 }
116
117 function startDhcp
118 {
119   local conf_file_="$DHCPD_CONFIG_FILE_"
120
121   test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
122   start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
123     --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
124 }
125
126 function runDhcp
127 {
128   isExistent "$DHCPD_CONFIG_FILE_" || \
129     warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
130
131   stopDhcp
132   sleep 1
133   startDhcp
134 }
135 # }}}
136
137 # IPTABLES {{{
138 function runIptables
139 {
140   if [[ $IPTABLES_SNAT_ != "true" ]]; then
141     return
142   fi
143   startIptables
144 }
145
146 function startIptables
147 {
148   if [ -x /sbin/iptables ] ; then
149     if [[ $NAT_INTERFACE_ != "" ]]; then
150        local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
151
152        if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
153           echo "Rule for SNAT already present, nothing to be done."
154        else
155           echo "Setting up SNAT for terminalserver clients on ${NAT_INTERFACE_}:"
156           echo    "* iptables -t nat -F POSTROUTING"
157           echo -n "* iptables -t nat -A POSTROUTING -o $NAT_INTERFACE_ -j SNAT --to-source $nat_source_ip_ ... "
158           { iptables -t nat -F POSTROUTING && \
159             iptables -t nat -A POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_" ; } && \
160             echo done || echo failed
161        fi
162        if [ `cat /proc/sys/net/ipv4/ip_forward` -eq 1 ]; then
163           echo "IP-Forwarding already enabled, nothing to be done."
164        else
165           echo -n "Enabling IP-Forwarding: "
166           echo 1 > /proc/sys/net/ipv4/ip_forward && echo done || echo failed
167        fi
168     fi
169   else
170     warn "iptables executable not avilable"
171   fi
172 }
173
174 function stopIptables
175 {
176   if [[ $IPTABLES_SNAT_ != "true" ]]; then
177     return
178   fi
179   if [ -x /sbin/iptables ] ; then
180     if [[ $NAT_INTERFACE_ != "" ]]; then
181        local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
182
183        if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
184          iptables -t nat -F POSTROUTING &>/dev/null && \
185            iptables -t nat -D POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_"
186        fi
187        echo 0 > /proc/sys/net/ipv4/ip_forward
188     fi
189   fi
190 }
191 # }}}
192
193 # TFTP SERVICE {{{
194 function removeTftpConf
195 {
196   rm -rf $TFTPD_DATA_DIR_/*
197 }
198 function createTftpConf
199 {
200   removeTftpConf
201
202   execute "mkdir -p $TFTPD_DATA_DIR_/pxelinux.cfg" die
203   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
204   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
205   if [ -d "$MOUNT_POINT_"/boot/release ] ; then
206     cp -r "$MOUNT_POINT_"/boot/release "$TFTPD_DATA_DIR_"
207   else
208     execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
209   fi
210   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
211   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
212   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
213
214   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
215 }
216
217 function stopTftp
218 {
219   start-stop-daemon --stop --quiet -p "$TFTPD_PID_"
220 }
221 function startTftp
222 {
223   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
224   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- --daemon --no-multicast --pidfile "$TFTPD_PID_" --bind-address "$IP_" "$TFTPD_DATA_DIR_" || \
225     warn "problems starting tftpd server"
226 }
227 function runTftp
228 {
229   stopTftp
230   sleep 1
231   startTftp
232 }
233 # }}}
234
235
236 # NFS  {{{
237 function createNfsConfig
238 {
239   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
240 }
241
242 function removeNfsConfig
243 {
244   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
245 }
246
247 function startNfs
248 {
249   /etc/init.d/portmap start
250   /etc/init.d/nfs-common start
251   # FIXME /etc/init.d/nfs-kernel-server start
252   $USR_SHARE_/nfs-kernel-server start
253   echo
254
255   createNfsConfig
256 }
257 function stopNfs
258 {
259   removeNfsConfig
260   if [[ `exportfs |wc -l` > 0 ]]; then
261     dprint "There are other exports, not stopping NFS serivces"
262   else
263     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
264     /etc/init.d/nfs-common stop >/dev/null 2>&1
265     killPortmapper
266   fi
267 }
268 # }}}
269
270
271 function createConfig
272 {
273   # FIXME
274   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
275   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
276
277   createTftpConf
278   createDhcpConf
279 }
280
281 function actionStart
282 {
283   createConfig
284
285   echo -n "Starting tftpd: "
286   runTftp && echo done || echo failed
287
288   echo -n "Starting dhcpd: "
289   runDhcp && echo done || echo failed
290
291   runIptables
292
293   echo "Finally starting nfs services..."
294   startNfs && echo "Successfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver failed!'
295 }
296
297 function actionStop
298 {
299   stopTftp
300   stopDhcp
301   stopIptables
302   stopNfs
303   notice "Terminal-server stopped"
304 }
305
306 function actionClean
307 {
308   actionStop
309   removeTftpConf
310   removeDhcpConf
311   stopNfs
312 }
313
314 function updateConfig
315 {
316   local service_="$1"
317
318   case "$service_" in
319     "") createConfig ;;
320     tftp) createTftpConf ;;
321     dhcp) createDhcpConf ;;
322     nfs) removeNfsConfig; createNfsConfig ;;
323     *) warn "Service $service_ not available" ;;
324   esac
325 }
326
327 # SERVICES {{{
328 function serviceStart
329 {
330   local service_="$1"   # service to start, if nothing => all
331
332   case "$service_" in
333     "") actionStart ;;
334     tftp) runTftp ;;
335     dhcp) runDhcp ;;
336     ipt) startIptables ;;
337     nfs) startNfs ;;
338     *) warn "Service $service_ not available" ;;
339   esac
340 }
341
342 function serviceStop
343 {
344   local service_="$1"   # service to stop, if nothing => all
345
346   case "$service_" in
347     "") actionStop ;;
348     tftp) stopTftp ;;
349     dhcp) stopDhcp ;;
350     ipt) stopIptables ;;
351     nfs) stopNfs ;;
352     *) warn "Service $service_ not available" ;;
353   esac
354 }
355 # }}}
356
357 ###
358 ### __MAIN
359 ###
360
361 while getopts "fi:hv" opt; do
362   case "$opt" in
363     f) FORCE_='true' ;;
364     h) printUsage; exit ;;
365     v) let verbose_=$verbose_+1 ;;
366     ?) printUsage; exit 64 ;;
367   esac
368 done
369 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
370 setVerbose $verbose_
371
372 case "$1" in
373   help)   printUsage; exit 0 ;;
374 esac
375
376 checkRoot die "You have to be root to use this program"
377 disableSyslog
378
379 isExistent $DEFAULT_CONFIG_ die
380 . $DEFAULT_CONFIG_
381 . $CONFIG_
382 # used config vars:
383 # MOUNT_POINT_
384 # KERNEL_IMAGE_
385 # MEMTEST_IMAGE_
386 # PXE_BOOT_MSG_
387 # PXE_BOOT_LOGO_
388 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
389   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
390   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
391 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
392 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
393 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
394 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
395   die "False configuration, please update $CONFIG_"
396 fi
397
398 case "$1" in
399   clean) actionClean; exit 0 ;;
400 esac
401
402
403 while true; do
404   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
405   if [ $? -eq 1 ]; then
406     $CONFIG_PROG_ && continue
407     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
408       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
409   else
410     break
411   fi
412 done
413 source $CONF_FILE_
414
415 # check for necessary files
416 check_necessary_files_='no'
417 if [ "$1" == 'start' ]; then
418   case "$2" in
419     tftp|"") check_necessary_files_='yes' ;;
420   esac
421 fi
422 if [ $check_necessary_files_ == 'yes' ]; then
423   # test for files absolutly necessary for grml-terminalserver and created from -config
424   problem_=0
425   for i in $PATH_/minirt26.gz; do
426     isExistent $i warn || problem_=1
427   done
428   if [ $problem_ -eq 1 ]; then
429     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
430   fi
431 fi
432
433
434 case "$1" in
435   start) serviceStart "$2" ;;
436   stop) serviceStop "$2" ;;
437   config) updateConfig "$2" ;;
438   "")  actionStart ;;
439   *)  printUsage ;;
440 esac
441
442 # END OF FILE
443 ################################################################################
444 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2