try to load all networking modules via linuxrc
[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  # something keeps answering all tftp requests with auth requests (SYN
139  # packets to the client tcp/113). Since the PXE client doesn't answer with
140  # RST, the auth query has to wait until it times out. Forbidding the
141  # terminalserver to send out packets to tcp/113 via iptables _greatly_
142  # speeds up the process. But of course the real fix would be to have grml
143  # stop sending out auth queries to tftp clients. according to netstat, it
144  # is in.tftpd itself sending out the auth queries.
145  # Thanks to Marc Haber and Wolfgang Karall for noticing and current fix.
146  if [ -x /sbin/iptables ] ; then
147     if iptables -L | grep -q '^REJECT.*tcp-reset' ; then
148        echo "Rule for tcp/113 already present, nothing to be done."
149     else
150        echo -n "Rejecting tcp/113 via iptables to speed up speed up booting via PXE: "
151        iptables -A OUTPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset && echo done || echo failed
152     fi
153  else
154     warn "iptables executable not avilable"
155  fi
156 }
157 # }}}
158
159 # TFTP SERVICE {{{
160 function removeTftpConf
161 {
162   rm -rf $TFTPD_DATA_DIR_/*
163 }
164 function createTftpConf
165 {
166   removeTftpConf
167
168   execute "mkdir $TFTPD_DATA_DIR_/pxelinux.cfg" die
169   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
170   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
171   execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
172   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
173   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
174   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
175
176   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
177 }
178
179 function stopTftp
180 {
181   start-stop-daemon --stop --quiet --name "${TFTPD_BIN_##*/}"
182 }
183 function startTftp
184 {
185   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
186   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- -l -a "$IP_" -s "$TFTPD_DATA_DIR_" || \
187     warn "problems starting tftpd server"
188 }
189 function runTftp
190 {
191   stopTftp
192   sleep 1
193   startTftp
194 }
195 # }}}
196
197
198 # NFS  {{{
199 function createNfsConfig
200 {
201   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
202 }
203
204 function removeNfsConfig
205 {
206   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
207 }
208
209 function startNfs
210 {
211   /etc/init.d/portmap start
212   /etc/init.d/nfs-common start
213   # FIXME /etc/init.d/nfs-kernel-server start
214   $USR_SHARE_/nfs-kernel-server start
215
216   createNfsConfig
217 }
218 function stopNfs
219 {
220   removeNfsConfig
221   if [[ `exportfs |wc -l` > 0 ]]; then
222     dprint "There are other exports, not stopping NFS serivces"
223   else
224     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
225     /etc/init.d/nfs-common stop >/dev/null 2>&1
226     killPortmapper
227   fi
228 }
229 # }}}
230
231
232 function createConfig
233 {
234   # FIXME
235   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
236   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
237
238   createTftpConf
239   createDhcpConf
240 }
241
242 function actionStart
243 {
244   createConfig
245
246   echo -n "Starting tftpd: "
247   runTftp && echo done || echo failed
248
249   echo -n "Starting dhcpd: "
250   runDhcp && echo done || echo failed
251
252   runIptables
253
254   echo "Starting nfs: "
255   startNfs && echo "Sucessfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver-config failed!'
256 }
257
258 function actionStop
259 {
260   stopTftp
261   stopDhcp
262   stopNfs
263   notice "Terminal-server stopped"
264 }
265
266 function actionClean
267 {
268   actionStop
269   removeTftpConf
270   removeDhcpConf
271   stopNfs
272 }
273
274 function updateConfig
275 {
276   local service_="$1"
277
278   case "$service_" in
279     "") createConfig ;;
280     tftp) createTftpConf ;;
281     dhcp) createDhcpConf ;;
282     nfs) removeNfsConfig; createNfsConfig ;;
283     *) warn "Service $service_ not available" ;;
284   esac
285 }
286
287 # SERVICES {{{
288 function serviceStart
289 {
290   local service_="$1"   # service to start, if nothing => all
291
292   case "$service_" in
293     "") actionStart ;;
294     tftp) runTftp ;;
295     dhcp) runDhcp ;;
296     nfs) startNfs ;;
297     *) warn "Service $service_ not available" ;;
298   esac
299 }
300
301 function serviceStop
302 {
303   local service_="$1"   # service to stop, if nothing => all
304
305   case "$service_" in
306     "") actionStop ;;
307     tftp) stopTftp ;;
308     dhcp) stopDhcp ;;
309     nfs) stopNfs ;;
310     *) warn "Service $service_ not available" ;;
311   esac
312 }
313 # }}}
314
315 ###
316 ### __MAIN
317 ###
318
319 while getopts "fi:hv" opt; do
320   case "$opt" in
321     f) FORCE_='true' ;;
322     h) printUsage; exit ;;
323     v) let verbose_=$verbose_+1 ;;
324     ?) printUsage; exit 64 ;;
325   esac
326 done
327 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
328 setVerbose $verbose_
329
330 case "$1" in
331   help)   printUsage; exit 0 ;;
332 esac
333
334 checkRoot die "You have to be root to use this program"
335 disableSyslog
336
337 isExistent $DEFAULT_CONFIG_ die
338 . $DEFAULT_CONFIG_
339 . $CONFIG_
340 # used config vars:
341 # MOUNT_POINT_
342 # KERNEL_IMAGE_
343 # MEMTEST_IMAGE_
344 # PXE_BOOT_MSG_
345 # PXE_BOOT_LOGO_
346 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
347   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
348   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
349 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
350 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
351 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
352 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
353   die "False configuration, please update $CONFIG_"
354 fi
355
356 case "$1" in
357   clean) actionClean; exit 0 ;;
358 esac
359
360
361 while true; do
362   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
363   if [ $? -eq 1 ]; then
364     $CONFIG_PROG_ && continue
365     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
366       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
367   else
368     break
369   fi
370 done
371 source $CONF_FILE_
372
373 # check for necessary files
374 check_necessary_files_='no'
375 if [ "$1" == 'start' ]; then
376   case "$2" in
377     tftp|"") check_necessary_files_='yes' ;;
378   esac
379 fi
380 if [ $check_necessary_files_ == 'yes' ]; then
381   # test for files absolutly necessary for grml-terminalserver and created from -config
382   problem_=0
383   for i in $PATH_/minirt26.gz; do
384     isExistent $i warn || problem_=1
385   done
386   if [ $problem_ -eq 1 ]; then
387     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
388   fi
389 fi
390
391
392 case "$1" in
393   start) serviceStart "$2" ;;
394   stop) serviceStop "$2" ;;
395   config) updateConfig "$2" ;;
396   "")  actionStart ;;
397   *)  printUsage ;;
398 esac
399
400 # END OF FILE
401 ################################################################################
402 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2