implemented a do not touch for dhcp config and a config action
[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 verbose_=0
24
25 # this file holds all variable definitions
26 SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
27 isExistent $SHARED_PROG_VARS_ die
28 . $SHARED_PROG_VARS_
29
30
31 ###
32 ### __FUNCTIONS
33 ###
34
35 function printUsage
36 {
37   cat <<EOT
38 Usage: "$PROG_NAME__" [OPTIONS] <command>
39
40 $PROG_NAME__ is the config program for the terminalserver
41 comming with grml.
42
43 COMMANDS:
44    help             This help text
45    start <service>  Start services (if all services are started, configs will be updated)
46    stop <service>   Stop services
47    config <service> Update config of given service (or from all if no services given)
48    clean            Stop all + remove config and boot files from services
49    <default>        interactive
50
51 SERVICES:
52   tftp        Tftp daemon
53   dhcp        Dhcp daemon
54   nfs         All necessary nfs daemons
55   <>          ALL services
56   
57 OPTIONS:
58    -v         verbose (show what is going on, v++)
59    -h         this help text
60
61 EOT
62 }
63
64 function killPortmapper
65 {
66     /etc/init.d/portmap stop >/dev/null &>/dev/null
67     killall -9 portmap &>/dev/null
68 }
69
70 # DHCP SERVICE {{{
71 function createDhcpConf
72 {
73   if [ -e "$DHCPD_CONFIG_FILE_" ]; then
74     if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
75       execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
76       execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
77     else
78       warn "Not updating user edited configfile $DHCPD_CONFIG_FILE_"
79     fi
80   else
81     execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
82   fi
83 }
84
85 function removeDhcpConf
86 {
87   if [ -e "$DHCPD_CONFIG_FILE_" ]; then
88     if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
89       rm -f "$DHCPD_CONFIG_FILE_"
90     else
91       warn "Not deleting user edited configfile $DHCPD_CONFIG_FILE_"
92     fi
93   fi
94 }
95
96
97 function stopDhcp
98 {
99   start-stop-daemon --stop --quiet --pidfile "$DHCPD_PID_"
100   rm -f $DHCPD_PID_
101   rm -f /var/lib/dhcp3/dhcpd.leases* 2>/dev/null    #FIXME
102   touch /var/lib/dhcp3/dhcpd.leases
103 }
104 function startDhcp
105 {
106   local conf_file_="$DHCPD_CONFIG_FILE_"
107   
108   test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
109   start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
110     --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
111 }
112 function runDhcp
113 {
114   isExistent "$DHCPD_CONFIG_FILE_" || \
115     warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
116
117   stopDhcp
118   sleep 1
119   startDhcp
120 }
121 # }}}
122
123 # TFTP SERVICE {{{
124 function removeTftpConf
125 {
126   rm -rf $TFTPD_DATA_DIR_/*
127 }
128 function createTftpConf
129 {
130   removeTftpConf
131   
132   execute "mkdir $TFTPD_DATA_DIR_/pxelinux.cfg" die
133   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
134   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
135   execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
136   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
137   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
138   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
139
140   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
141 }
142
143 function stopTftp
144 {
145   start-stop-daemon --stop --quiet --name "${TFTPD_BIN_##*/}"
146 }
147 function startTftp
148 {
149   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
150   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- -l -a "$IP_" -s "$TFTPD_DATA_DIR_" || \
151     warn "problems starting tftpd server"
152 }
153 function runTftp
154 {
155   stopTftp
156   sleep 1
157   startTftp
158 }
159 # }}}
160
161
162 # NFS  {{{
163 function createNfsConfig
164 {
165   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
166 }
167
168 function removeNfsConfig
169 {
170   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
171 }
172
173 function startNfs
174 {
175   /etc/init.d/portmap start
176   /etc/init.d/nfs-common start
177   # FIXME /etc/init.d/nfs-kernel-server start
178   $USR_SHARE_/nfs-kernel-server start
179
180   createNfsConfig
181 }
182 function stopNfs
183 {
184   removeNfsConfig
185   if [[ `exportfs |wc -l` > 0 ]]; then
186     dprint "There are other exports, not stopping NFS serivces"
187   else
188     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
189     /etc/init.d/nfs-common stop >/dev/null 2>&1
190     killPortmapper
191   fi
192 }
193 # }}}
194
195 function allreadyConfigured
196 {
197   isExistent "$CONF_FILE_" dprint || return 1
198   return 0
199 }
200
201 function createConfig
202 {
203   # FIXME
204   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
205   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
206
207   createTftpConf
208   createDhcpConf
209 }
210
211 function actionStart
212 {
213   createConfig
214   
215   echo -n "Starting tftpd..."
216   runTftp
217   echo "done"
218   echo -n "Starting dhcpd..."
219   runDhcp
220   echo "done"
221   echo -n "Starting nfs..."
222   startNfs
223   echo "done"
224 }
225
226 function actionStop
227 {
228   stopTftp
229   stopDhcp
230   stopNfs
231   notice "Terminal-server stopped"
232 }
233
234 function actionClean
235 {
236   actionStop
237   removeTftpConf
238   removeDhcpConf
239   stopNfs
240 }
241
242 function updateConfig
243 {
244   local service_="$1"
245
246   case "$service_" in
247     "") createConfig ;;
248     tftp) createTftpConf ;;
249     dhcp) createDhcpConf ;;
250     nfs) removeNfsConfig; createNfsConfig ;;
251     *) warn "Service $service_ not available" ;;
252   esac
253 }
254
255 # SERVICES {{{
256 function serviceStart
257 {
258   local service_="$1"   # service to start, if nothing => all
259
260   case "$service_" in
261     "") actionStart ;;
262     tftp) runTftp ;;
263     dhcp) runDhcp ;;
264     nfs) startNfs ;;
265     *) warn "Service $service_ not available" ;;
266   esac
267 }
268
269 function serviceStop
270 {
271   local service_="$1"   # service to stop, if nothing => all
272
273   case "$service_" in
274     "") actionStop ;;
275     tftp) stopTftp ;;
276     dhcp) stopDhcp ;;
277     nfs) stopNfs ;;
278     *) warn "Service $service_ not available" ;;
279   esac
280 }
281 # }}}
282
283 ###
284 ### __MAIN
285 ###
286
287 while getopts "i:hv" opt; do
288   case "$opt" in
289     h) printUsage; exit ;;
290     v) let verbose_=$verbose_+1 ;;
291     ?) printUsage; exit 64 ;;
292   esac
293 done
294 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
295 setVerbose $verbose_
296
297 case "$1" in
298   help)   printUsage; exit 0 ;;
299 esac
300
301 checkRoot die "You have to be root to use this program"
302 disableSyslog
303
304 isExistent $DEFAULT_CONFIG_ die
305 . $DEFAULT_CONFIG_
306 . $CONFIG_
307 # used config vars:
308 # MOUNT_POINT_
309 # KERNEL_IMAGE_
310 # MEMTEST_IMAGE_
311 # PXE_BOOT_MSG_
312 # PXE_BOOT_LOGO_
313 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
314   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
315   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
316 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
317 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
318 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
319 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
320   die "False configuration, please update $CONFIG_"
321 fi
322   
323 case "$1" in
324   clean) actionClean; exit 0 ;;
325 esac
326
327
328 while true; do
329   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
330   if [ $? -eq 1 ]; then
331     $CONFIG_PROG_ && continue
332     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
333       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
334   else
335     break
336   fi  
337 done
338 source $CONF_FILE_
339
340 # check for necessary files
341 check_necessary_files_='no'
342 if [ "$1" == 'start' ]; then
343   case "$2" in
344     tftp|"") check_necessary_files_='yes' ;;
345   esac
346 fi
347 if [ $check_necessary_files_ == 'yes' ]; then
348   # test for files absolutly necessary for grml-terminalserver and created from -config
349   problem_=0
350   for i in $PATH_/minirt26.gz; do
351     isExistent $i warn || problem_=1
352   done
353   if [ $problem_ -eq 1 ]; then
354     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
355   fi
356 fi
357
358
359 case "$1" in
360   start) serviceStart "$2" ;;
361   stop) serviceStop "$2" ;;
362   config) updateConfig "$2" ;;
363   "")  actionStart ;;
364   *)  printUsage ;;
365 esac
366
367 # END OF FILE
368 ################################################################################
369 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2