type fixes
[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 function startDhcp
116 {
117   local conf_file_="$DHCPD_CONFIG_FILE_"
118   
119   test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
120   start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
121     --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
122 }
123 function runDhcp
124 {
125   isExistent "$DHCPD_CONFIG_FILE_" || \
126     warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
127
128   stopDhcp
129   sleep 1
130   startDhcp
131 }
132 # }}}
133
134 # TFTP SERVICE {{{
135 function removeTftpConf
136 {
137   rm -rf $TFTPD_DATA_DIR_/*
138 }
139 function createTftpConf
140 {
141   removeTftpConf
142   
143   execute "mkdir $TFTPD_DATA_DIR_/pxelinux.cfg" die
144   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
145   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
146   execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
147   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
148   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
149   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
150
151   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
152 }
153
154 function stopTftp
155 {
156   start-stop-daemon --stop --quiet --name "${TFTPD_BIN_##*/}"
157 }
158 function startTftp
159 {
160   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
161   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- -l -a "$IP_" -s "$TFTPD_DATA_DIR_" || \
162     warn "problems starting tftpd server"
163 }
164 function runTftp
165 {
166   stopTftp
167   sleep 1
168   startTftp
169 }
170 # }}}
171
172
173 # NFS  {{{
174 function createNfsConfig
175 {
176   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
177 }
178
179 function removeNfsConfig
180 {
181   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
182 }
183
184 function startNfs
185 {
186   /etc/init.d/portmap start
187   /etc/init.d/nfs-common start
188   # FIXME /etc/init.d/nfs-kernel-server start
189   $USR_SHARE_/nfs-kernel-server start
190
191   createNfsConfig
192 }
193 function stopNfs
194 {
195   removeNfsConfig
196   if [[ `exportfs |wc -l` > 0 ]]; then
197     dprint "There are other exports, not stopping NFS serivces"
198   else
199     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
200     /etc/init.d/nfs-common stop >/dev/null 2>&1
201     killPortmapper
202   fi
203 }
204 # }}}
205
206
207 function createConfig
208 {
209   # FIXME
210   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
211   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
212
213   createTftpConf
214   createDhcpConf
215 }
216
217 function actionStart
218 {
219   createConfig
220   
221   echo -n "Starting tftpd..."
222   runTftp
223   echo "done"
224   echo -n "Starting dhcpd..."
225   runDhcp
226   echo "done"
227   echo -n "Starting nfs..."
228   startNfs
229   echo "done"
230 }
231
232 function actionStop
233 {
234   stopTftp
235   stopDhcp
236   stopNfs
237   notice "Terminal-server stopped"
238 }
239
240 function actionClean
241 {
242   actionStop
243   removeTftpConf
244   removeDhcpConf
245   stopNfs
246 }
247
248 function updateConfig
249 {
250   local service_="$1"
251
252   case "$service_" in
253     "") createConfig ;;
254     tftp) createTftpConf ;;
255     dhcp) createDhcpConf ;;
256     nfs) removeNfsConfig; createNfsConfig ;;
257     *) warn "Service $service_ not available" ;;
258   esac
259 }
260
261 # SERVICES {{{
262 function serviceStart
263 {
264   local service_="$1"   # service to start, if nothing => all
265
266   case "$service_" in
267     "") actionStart ;;
268     tftp) runTftp ;;
269     dhcp) runDhcp ;;
270     nfs) startNfs ;;
271     *) warn "Service $service_ not available" ;;
272   esac
273 }
274
275 function serviceStop
276 {
277   local service_="$1"   # service to stop, if nothing => all
278
279   case "$service_" in
280     "") actionStop ;;
281     tftp) stopTftp ;;
282     dhcp) stopDhcp ;;
283     nfs) stopNfs ;;
284     *) warn "Service $service_ not available" ;;
285   esac
286 }
287 # }}}
288
289 ###
290 ### __MAIN
291 ###
292
293 while getopts "fi:hv" opt; do
294   case "$opt" in
295     f) FORCE_='true' ;;
296     h) printUsage; exit ;;
297     v) let verbose_=$verbose_+1 ;;
298     ?) printUsage; exit 64 ;;
299   esac
300 done
301 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
302 setVerbose $verbose_
303
304 case "$1" in
305   help)   printUsage; exit 0 ;;
306 esac
307
308 checkRoot die "You have to be root to use this program"
309 disableSyslog
310
311 isExistent $DEFAULT_CONFIG_ die
312 . $DEFAULT_CONFIG_
313 . $CONFIG_
314 # used config vars:
315 # MOUNT_POINT_
316 # KERNEL_IMAGE_
317 # MEMTEST_IMAGE_
318 # PXE_BOOT_MSG_
319 # PXE_BOOT_LOGO_
320 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
321   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
322   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
323 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
324 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
325 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
326 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
327   die "False configuration, please update $CONFIG_"
328 fi
329   
330 case "$1" in
331   clean) actionClean; exit 0 ;;
332 esac
333
334
335 while true; do
336   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
337   if [ $? -eq 1 ]; then
338     $CONFIG_PROG_ && continue
339     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
340       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
341   else
342     break
343   fi  
344 done
345 source $CONF_FILE_
346
347 # check for necessary files
348 check_necessary_files_='no'
349 if [ "$1" == 'start' ]; then
350   case "$2" in
351     tftp|"") check_necessary_files_='yes' ;;
352   esac
353 fi
354 if [ $check_necessary_files_ == 'yes' ]; then
355   # test for files absolutly necessary for grml-terminalserver and created from -config
356   problem_=0
357   for i in $PATH_/minirt26.gz; do
358     isExistent $i warn || problem_=1
359   done
360   if [ $problem_ -eq 1 ]; then
361     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
362   fi
363 fi
364
365
366 case "$1" in
367   start) serviceStart "$2" ;;
368   stop) serviceStop "$2" ;;
369   config) updateConfig "$2" ;;
370   "")  actionStart ;;
371   *)  printUsage ;;
372 esac
373
374 # END OF FILE
375 ################################################################################
376 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2