Iterate over /var/lib/dhcp* directories for touch command.
[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   # ugly but necessary :-/
114   find  /var/lib/dhcp* -type f -name dhcpd.leases -delete
115   local dhcpdir
116   for dhcpdir in /var/lib/dhcp* ; do
117     touch ${dhcpdir}/dhcpd.leases
118   done
119 }
120
121 function startDhcp
122 {
123   local conf_file_="$DHCPD_CONFIG_FILE_"
124
125   test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
126   start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
127     --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
128 }
129
130 function runDhcp
131 {
132   isExistent "$DHCPD_CONFIG_FILE_" || \
133     warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
134
135   stopDhcp
136   sleep 1
137   startDhcp
138 }
139 # }}}
140
141 # IPTABLES {{{
142 function runIptables
143 {
144   if [[ $IPTABLES_SNAT_ != "true" ]]; then
145     return
146   fi
147   startIptables
148 }
149
150 function startIptables
151 {
152   if [ -x /sbin/iptables ] ; then
153     if [[ $NAT_INTERFACE_ != "" ]]; then
154        local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
155
156        if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
157           echo "Rule for SNAT already present, nothing to be done."
158        else
159           echo "Setting up SNAT for terminalserver clients on ${NAT_INTERFACE_}:"
160           echo    "* iptables -t nat -F POSTROUTING"
161           echo -n "* iptables -t nat -A POSTROUTING -o $NAT_INTERFACE_ -j SNAT --to-source $nat_source_ip_ ... "
162           { iptables -t nat -F POSTROUTING && \
163             iptables -t nat -A POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_" ; } && \
164             echo done || echo failed
165        fi
166        if [ `cat /proc/sys/net/ipv4/ip_forward` -eq 1 ]; then
167           echo "IP-Forwarding already enabled, nothing to be done."
168        else
169           echo -n "Enabling IP-Forwarding: "
170           echo 1 > /proc/sys/net/ipv4/ip_forward && echo done || echo failed
171        fi
172     fi
173   else
174     warn "iptables executable not avilable"
175   fi
176 }
177
178 function stopIptables
179 {
180   if [[ $IPTABLES_SNAT_ != "true" ]]; then
181     return
182   fi
183   if [ -x /sbin/iptables ] ; then
184     if [[ $NAT_INTERFACE_ != "" ]]; then
185        local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
186
187        if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
188          iptables -t nat -F POSTROUTING &>/dev/null && \
189            iptables -t nat -D POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_"
190        fi
191        echo 0 > /proc/sys/net/ipv4/ip_forward
192     fi
193   fi
194 }
195 # }}}
196
197 # TFTP SERVICE {{{
198 function removeTftpConf
199 {
200   rm -rf $TFTPD_DATA_DIR_/*
201 }
202 function createTftpConf
203 {
204   removeTftpConf
205
206   execute "mkdir -p $TFTPD_DATA_DIR_/pxelinux.cfg" die
207   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
208   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
209   if [ -d "$MOUNT_POINT_"/boot/release ] ; then
210     cp -r "$MOUNT_POINT_"/boot/release "$TFTPD_DATA_DIR_"
211   else
212     execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
213   fi
214   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
215   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
216   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
217
218   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
219 }
220
221 function stopTftp
222 {
223   start-stop-daemon --stop --quiet -p "$TFTPD_PID_"
224 }
225 function startTftp
226 {
227   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
228   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- --daemon --no-multicast --pidfile "$TFTPD_PID_" --bind-address "$IP_" "$TFTPD_DATA_DIR_" || \
229     warn "problems starting tftpd server"
230 }
231 function runTftp
232 {
233   stopTftp
234   sleep 1
235   startTftp
236 }
237 # }}}
238
239
240 # NFS  {{{
241 function createNfsConfig
242 {
243   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
244 }
245
246 function removeNfsConfig
247 {
248   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
249 }
250
251 function startNfs
252 {
253   /etc/init.d/portmap start
254   /etc/init.d/nfs-common start
255   # FIXME /etc/init.d/nfs-kernel-server start
256   $USR_SHARE_/nfs-kernel-server start
257   echo
258
259   createNfsConfig
260 }
261 function stopNfs
262 {
263   removeNfsConfig
264   if [[ `exportfs |wc -l` > 0 ]]; then
265     dprint "There are other exports, not stopping NFS serivces"
266   else
267     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
268     /etc/init.d/nfs-common stop >/dev/null 2>&1
269     killPortmapper
270   fi
271 }
272 # }}}
273
274
275 function createConfig
276 {
277   # FIXME
278   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
279   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
280
281   createTftpConf
282   createDhcpConf
283 }
284
285 function actionStart
286 {
287   createConfig
288
289   echo -n "Starting tftpd: "
290   runTftp && echo done || echo failed
291
292   echo -n "Starting dhcpd: "
293   runDhcp && echo done || echo failed
294
295   runIptables
296
297   echo "Finally starting nfs services..."
298   startNfs && echo "Successfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver failed!'
299 }
300
301 function actionStop
302 {
303   stopTftp
304   stopDhcp
305   stopIptables
306   stopNfs
307   notice "Terminal-server stopped"
308 }
309
310 function actionClean
311 {
312   actionStop
313   removeTftpConf
314   removeDhcpConf
315   stopNfs
316 }
317
318 function updateConfig
319 {
320   local service_="$1"
321
322   case "$service_" in
323     "") createConfig ;;
324     tftp) createTftpConf ;;
325     dhcp) createDhcpConf ;;
326     nfs) removeNfsConfig; createNfsConfig ;;
327     *) warn "Service $service_ not available" ;;
328   esac
329 }
330
331 # SERVICES {{{
332 function serviceStart
333 {
334   local service_="$1"   # service to start, if nothing => all
335
336   case "$service_" in
337     "") actionStart ;;
338     tftp) runTftp ;;
339     dhcp) runDhcp ;;
340     ipt) startIptables ;;
341     nfs) startNfs ;;
342     *) warn "Service $service_ not available" ;;
343   esac
344 }
345
346 function serviceStop
347 {
348   local service_="$1"   # service to stop, if nothing => all
349
350   case "$service_" in
351     "") actionStop ;;
352     tftp) stopTftp ;;
353     dhcp) stopDhcp ;;
354     ipt) stopIptables ;;
355     nfs) stopNfs ;;
356     *) warn "Service $service_ not available" ;;
357   esac
358 }
359 # }}}
360
361 ###
362 ### __MAIN
363 ###
364
365 while getopts "fi:hv" opt; do
366   case "$opt" in
367     f) FORCE_='true' ;;
368     h) printUsage; exit ;;
369     v) let verbose_=$verbose_+1 ;;
370     ?) printUsage; exit 64 ;;
371   esac
372 done
373 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
374 setVerbose $verbose_
375
376 case "$1" in
377   help)   printUsage; exit 0 ;;
378 esac
379
380 checkRoot die "You have to be root to use this program"
381 disableSyslog
382
383 isExistent $DEFAULT_CONFIG_ die
384 . $DEFAULT_CONFIG_
385 . $CONFIG_
386 # used config vars:
387 # MOUNT_POINT_
388 # KERNEL_IMAGE_
389 # MEMTEST_IMAGE_
390 # PXE_BOOT_MSG_
391 # PXE_BOOT_LOGO_
392 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
393   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
394   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
395 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
396 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
397 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
398 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
399   die "False configuration, please update $CONFIG_"
400 fi
401
402 case "$1" in
403   clean) actionClean; exit 0 ;;
404 esac
405
406
407 while true; do
408   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
409   if [ $? -eq 1 ]; then
410     $CONFIG_PROG_ && continue
411     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
412       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
413   else
414     break
415   fi
416 done
417 source $CONF_FILE_
418
419 # check for necessary files
420 check_necessary_files_='no'
421 if [ "$1" == 'start' ]; then
422   case "$2" in
423     tftp|"") check_necessary_files_='yes' ;;
424   esac
425 fi
426 if [ $check_necessary_files_ == 'yes' ]; then
427   # test for files absolutly necessary for grml-terminalserver and created from -config
428   problem_=0
429   for i in $PATH_/minirt26.gz; do
430     isExistent $i warn || problem_=1
431   done
432   if [ $problem_ -eq 1 ]; then
433     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
434   fi
435 fi
436
437
438 case "$1" in
439   start) serviceStart "$2" ;;
440   stop) serviceStop "$2" ;;
441   config) updateConfig "$2" ;;
442   "")  actionStart ;;
443   *)  printUsage ;;
444 esac
445
446 # END OF FILE
447 ################################################################################
448 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2