Added tag 0.98 for changeset 7b957b5b0c56
[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   execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
206   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
207   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
208   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
209
210   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
211 }
212
213 function stopTftp
214 {
215   start-stop-daemon --stop --quiet -p "$TFTPD_PID_"
216 }
217 function startTftp
218 {
219   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
220   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- --daemon --no-multicast --pidfile "$TFTPD_PID_" --bind-address "$IP_" "$TFTPD_DATA_DIR_" || \
221     warn "problems starting tftpd server"
222 }
223 function runTftp
224 {
225   stopTftp
226   sleep 1
227   startTftp
228 }
229 # }}}
230
231
232 # NFS  {{{
233 function createNfsConfig
234 {
235   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
236 }
237
238 function removeNfsConfig
239 {
240   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
241 }
242
243 function startNfs
244 {
245   /etc/init.d/portmap start
246   /etc/init.d/nfs-common start
247   # FIXME /etc/init.d/nfs-kernel-server start
248   $USR_SHARE_/nfs-kernel-server start
249
250   createNfsConfig
251 }
252 function stopNfs
253 {
254   removeNfsConfig
255   if [[ `exportfs |wc -l` > 0 ]]; then
256     dprint "There are other exports, not stopping NFS serivces"
257   else
258     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
259     /etc/init.d/nfs-common stop >/dev/null 2>&1
260     killPortmapper
261   fi
262 }
263 # }}}
264
265
266 function createConfig
267 {
268   # FIXME
269   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
270   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
271
272   createTftpConf
273   createDhcpConf
274 }
275
276 function actionStart
277 {
278   createConfig
279
280   echo -n "Starting tftpd: "
281   runTftp && echo done || echo failed
282
283   echo -n "Starting dhcpd: "
284   runDhcp && echo done || echo failed
285
286   runIptables
287
288   echo "Finally starting nfs services..."
289   startNfs && echo "Sucessfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver failed!'
290 }
291
292 function actionStop
293 {
294   stopTftp
295   stopDhcp
296   stopIptables
297   stopNfs
298   notice "Terminal-server stopped"
299 }
300
301 function actionClean
302 {
303   actionStop
304   removeTftpConf
305   removeDhcpConf
306   stopNfs
307 }
308
309 function updateConfig
310 {
311   local service_="$1"
312
313   case "$service_" in
314     "") createConfig ;;
315     tftp) createTftpConf ;;
316     dhcp) createDhcpConf ;;
317     nfs) removeNfsConfig; createNfsConfig ;;
318     *) warn "Service $service_ not available" ;;
319   esac
320 }
321
322 # SERVICES {{{
323 function serviceStart
324 {
325   local service_="$1"   # service to start, if nothing => all
326
327   case "$service_" in
328     "") actionStart ;;
329     tftp) runTftp ;;
330     dhcp) runDhcp ;;
331     ipt) startIptables ;;
332     nfs) startNfs ;;
333     *) warn "Service $service_ not available" ;;
334   esac
335 }
336
337 function serviceStop
338 {
339   local service_="$1"   # service to stop, if nothing => all
340
341   case "$service_" in
342     "") actionStop ;;
343     tftp) stopTftp ;;
344     dhcp) stopDhcp ;;
345     ipt) stopIptables ;;
346     nfs) stopNfs ;;
347     *) warn "Service $service_ not available" ;;
348   esac
349 }
350 # }}}
351
352 ###
353 ### __MAIN
354 ###
355
356 while getopts "fi:hv" opt; do
357   case "$opt" in
358     f) FORCE_='true' ;;
359     h) printUsage; exit ;;
360     v) let verbose_=$verbose_+1 ;;
361     ?) printUsage; exit 64 ;;
362   esac
363 done
364 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
365 setVerbose $verbose_
366
367 case "$1" in
368   help)   printUsage; exit 0 ;;
369 esac
370
371 checkRoot die "You have to be root to use this program"
372 disableSyslog
373
374 isExistent $DEFAULT_CONFIG_ die
375 . $DEFAULT_CONFIG_
376 . $CONFIG_
377 # used config vars:
378 # MOUNT_POINT_
379 # KERNEL_IMAGE_
380 # MEMTEST_IMAGE_
381 # PXE_BOOT_MSG_
382 # PXE_BOOT_LOGO_
383 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
384   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
385   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
386 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
387 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
388 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
389 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
390   die "False configuration, please update $CONFIG_"
391 fi
392
393 case "$1" in
394   clean) actionClean; exit 0 ;;
395 esac
396
397
398 while true; do
399   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
400   if [ $? -eq 1 ]; then
401     $CONFIG_PROG_ && continue
402     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
403       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
404   else
405     break
406   fi
407 done
408 source $CONF_FILE_
409
410 # check for necessary files
411 check_necessary_files_='no'
412 if [ "$1" == 'start' ]; then
413   case "$2" in
414     tftp|"") check_necessary_files_='yes' ;;
415   esac
416 fi
417 if [ $check_necessary_files_ == 'yes' ]; then
418   # test for files absolutly necessary for grml-terminalserver and created from -config
419   problem_=0
420   for i in $PATH_/minirt26.gz; do
421     isExistent $i warn || problem_=1
422   done
423   if [ $problem_ -eq 1 ]; then
424     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
425   fi
426 fi
427
428
429 case "$1" in
430   start) serviceStart "$2" ;;
431   stop) serviceStop "$2" ;;
432   config) updateConfig "$2" ;;
433   "")  actionStart ;;
434   *)  printUsage ;;
435 esac
436
437 # END OF FILE
438 ################################################################################
439 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2