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