* Applied patch from Wolfgang Karall which:
[grml-terminalserver.git] / grml-terminalserver
1 #!/bin/sh
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   nfs         All necessary nfs daemons
56   <>          ALL services
57
58 OPTIONS:
59    -v         verbose (show what is going on, v++)
60    -h         this help text
61    -f         Force
62
63 EOT
64 }
65
66 function killPortmapper
67 {
68     /etc/init.d/portmap stop >/dev/null &>/dev/null
69     killall -9 portmap &>/dev/null
70 }
71
72 # DHCP SERVICE {{{
73 function createDhcpConf
74 {
75   if [ -e "$DHCPD_CONFIG_FILE_" ]; then
76     if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
77       execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
78       execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
79     else
80       if [[ $FORCE_ == "true" ]]; then
81         execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
82         execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
83       else
84         warn "Not updating user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
85       fi
86     fi
87   else
88     execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
89   fi
90 }
91
92 function removeDhcpConf
93 {
94   if [ -e "$DHCPD_CONFIG_FILE_" ]; then
95     if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
96       rm -f "$DHCPD_CONFIG_FILE_"
97     else
98       if [[ $FORCE_ == "true" ]]; then
99         rm -f "$DHCPD_CONFIG_FILE_"
100       else
101         warn "Not deleting user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
102       fi
103     fi
104   fi
105 }
106
107
108 function stopDhcp
109 {
110   start-stop-daemon --stop --quiet --pidfile "$DHCPD_PID_"
111   rm -f $DHCPD_PID_
112   rm -f /var/lib/dhcp3/dhcpd.leases* 2>/dev/null    #FIXME
113   touch /var/lib/dhcp3/dhcpd.leases
114 }
115
116 function startDhcp
117 {
118   local conf_file_="$DHCPD_CONFIG_FILE_"
119
120   test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
121   start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
122     --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
123 }
124
125 function runDhcp
126 {
127   isExistent "$DHCPD_CONFIG_FILE_" || \
128     warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
129
130   stopDhcp
131   sleep 1
132   startDhcp
133 }
134
135 # make sure tcp/113 is rejected
136 function runIptables
137 {
138  if [ -x /sbin/iptables ] ; then
139     # something keeps answering all tftp requests with auth requests (SYN
140     # packets to the client tcp/113). Since the PXE client doesn't answer with
141     # RST, the auth query has to wait until it times out. Forbidding the
142     # terminalserver to send out packets to tcp/113 via iptables _greatly_
143     # speeds up the process. But of course the real fix would be to have grml
144     # stop sending out auth queries to tftp clients. according to netstat, it
145     # is in.tftpd itself sending out the auth queries.
146     # Thanks to Marc Haber and Wolfgang Karall for noticing and current fix.
147     if iptables -L | grep -q '^REJECT.*tcp dpt:auth reject-with tcp-reset' ; then
148        echo "Rule for tcp/113 already present, nothing to be done."
149     else
150        echo "Rejecting tcp/113 via iptables to speed up booting via PXE, running:"
151        echo -n '* iptables -A OUTPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset ... '
152        iptables -A OUTPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset && echo done || echo failed
153     fi
154     # deactivate Multicast-DNS
155     if iptables -L | grep -q '^REJECT.*dpt:mdns reject-with icmp-port-unreachable' ; then
156        echo "Rule for udp/5353 already present, nothing to be done."
157     else
158        echo "Rejecting udp/5353 via iptables for deactivating Multicast-DNS, running:"
159        echo -n '* iptables -A OUTPUT -p udp -d 224.0.0.0/8 --dport 5353 -j REJECT ... '
160        iptables -A OUTPUT -p udp -d 224.0.0.0/8 --dport 5353 -j REJECT && echo done || echo failed
161     fi
162     if [ "$NAT_INTERFACE_" != "none" ]; then
163        local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
164
165        if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
166           echo "Rule for SNAT already present, nothing to be done."
167        else
168           echo "Setting up SNAT for terminalserver clients on ${NAT_INTERFACE_}:"
169           echo    "* iptables -t nat -F POSTROUTING"
170           echo -n "* iptables -t nat -A POSTROUTING -o $NAT_INTERFACE_ -j SNAT --to-source $nat_source_ip_ ... "
171           { iptables -t nat -F POSTROUTING && \
172             iptables -t nat -A POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_" ; } && \
173             echo done || echo failed
174        fi
175        if [ `cat /proc/sys/net/ipv4/ip_forward` -eq 1 ]; then
176           echo "IP-Forwarding already enabled, nothing to be done."
177        else
178           echo -n "Enabling IP-Forwarding: "
179           echo 1 > /proc/sys/net/ipv4/ip_forward && echo done || echo failed
180        fi
181     fi
182  else
183     warn "iptables executable not avilable"
184  fi
185 }
186 # }}}
187
188 # TFTP SERVICE {{{
189 function removeTftpConf
190 {
191   rm -rf $TFTPD_DATA_DIR_/*
192 }
193 function createTftpConf
194 {
195   removeTftpConf
196
197   execute "mkdir $TFTPD_DATA_DIR_/pxelinux.cfg" die
198   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
199   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
200   execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
201   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
202   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
203   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
204
205   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
206 }
207
208 function stopTftp
209 {
210   start-stop-daemon --stop --quiet --name "${TFTPD_BIN_##*/}"
211 }
212 function startTftp
213 {
214   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
215   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- -l -a "$IP_" -s "$TFTPD_DATA_DIR_" || \
216     warn "problems starting tftpd server"
217 }
218 function runTftp
219 {
220   stopTftp
221   sleep 1
222   startTftp
223 }
224 # }}}
225
226
227 # NFS  {{{
228 function createNfsConfig
229 {
230   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
231 }
232
233 function removeNfsConfig
234 {
235   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
236 }
237
238 function startNfs
239 {
240   /etc/init.d/portmap start
241   /etc/init.d/nfs-common start
242   # FIXME /etc/init.d/nfs-kernel-server start
243   $USR_SHARE_/nfs-kernel-server start
244
245   createNfsConfig
246 }
247 function stopNfs
248 {
249   removeNfsConfig
250   if [[ `exportfs |wc -l` > 0 ]]; then
251     dprint "There are other exports, not stopping NFS serivces"
252   else
253     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
254     /etc/init.d/nfs-common stop >/dev/null 2>&1
255     killPortmapper
256   fi
257 }
258 # }}}
259
260
261 function createConfig
262 {
263   # FIXME
264   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
265   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
266
267   createTftpConf
268   createDhcpConf
269 }
270
271 function actionStart
272 {
273   createConfig
274
275   echo -n "Starting tftpd: "
276   runTftp && echo done || echo failed
277
278   echo -n "Starting dhcpd: "
279   runDhcp && echo done || echo failed
280
281   runIptables
282
283   echo "Finally starting nfs services..."
284   startNfs && echo "Sucessfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver-config failed!'
285 }
286
287 function actionStop
288 {
289   stopTftp
290   stopDhcp
291   stopNfs
292   notice "Terminal-server stopped"
293 }
294
295 function actionClean
296 {
297   actionStop
298   removeTftpConf
299   removeDhcpConf
300   stopNfs
301 }
302
303 function updateConfig
304 {
305   local service_="$1"
306
307   case "$service_" in
308     "") createConfig ;;
309     tftp) createTftpConf ;;
310     dhcp) createDhcpConf ;;
311     nfs) removeNfsConfig; createNfsConfig ;;
312     *) warn "Service $service_ not available" ;;
313   esac
314 }
315
316 # SERVICES {{{
317 function serviceStart
318 {
319   local service_="$1"   # service to start, if nothing => all
320
321   case "$service_" in
322     "") actionStart ;;
323     tftp) runTftp ;;
324     dhcp) runDhcp ;;
325     nfs) startNfs ;;
326     *) warn "Service $service_ not available" ;;
327   esac
328 }
329
330 function serviceStop
331 {
332   local service_="$1"   # service to stop, if nothing => all
333
334   case "$service_" in
335     "") actionStop ;;
336     tftp) stopTftp ;;
337     dhcp) stopDhcp ;;
338     nfs) stopNfs ;;
339     *) warn "Service $service_ not available" ;;
340   esac
341 }
342 # }}}
343
344 ###
345 ### __MAIN
346 ###
347
348 while getopts "fi:hv" opt; do
349   case "$opt" in
350     f) FORCE_='true' ;;
351     h) printUsage; exit ;;
352     v) let verbose_=$verbose_+1 ;;
353     ?) printUsage; exit 64 ;;
354   esac
355 done
356 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
357 setVerbose $verbose_
358
359 case "$1" in
360   help)   printUsage; exit 0 ;;
361 esac
362
363 checkRoot die "You have to be root to use this program"
364 disableSyslog
365
366 isExistent $DEFAULT_CONFIG_ die
367 . $DEFAULT_CONFIG_
368 . $CONFIG_
369 # used config vars:
370 # MOUNT_POINT_
371 # KERNEL_IMAGE_
372 # MEMTEST_IMAGE_
373 # PXE_BOOT_MSG_
374 # PXE_BOOT_LOGO_
375 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
376   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
377   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
378 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
379 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
380 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
381 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
382   die "False configuration, please update $CONFIG_"
383 fi
384
385 case "$1" in
386   clean) actionClean; exit 0 ;;
387 esac
388
389
390 while true; do
391   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
392   if [ $? -eq 1 ]; then
393     $CONFIG_PROG_ && continue
394     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
395       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
396   else
397     break
398   fi
399 done
400 source $CONF_FILE_
401
402 # check for necessary files
403 check_necessary_files_='no'
404 if [ "$1" == 'start' ]; then
405   case "$2" in
406     tftp|"") check_necessary_files_='yes' ;;
407   esac
408 fi
409 if [ $check_necessary_files_ == 'yes' ]; then
410   # test for files absolutly necessary for grml-terminalserver and created from -config
411   problem_=0
412   for i in $PATH_/minirt26.gz; do
413     isExistent $i warn || problem_=1
414   done
415   if [ $problem_ -eq 1 ]; then
416     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
417   fi
418 fi
419
420
421 case "$1" in
422   start) serviceStart "$2" ;;
423   stop) serviceStop "$2" ;;
424   config) updateConfig "$2" ;;
425   "")  actionStart ;;
426   *)  printUsage ;;
427 esac
428
429 # END OF FILE
430 ################################################################################
431 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2