added -f FORCE option, to override the do not touch in configs
[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 function allreadyConfigured
207 {
208   isExistent "$CONF_FILE_" dprint || return 1
209   return 0
210 }
211
212 function createConfig
213 {
214   # FIXME
215   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
216   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
217
218   createTftpConf
219   createDhcpConf
220 }
221
222 function actionStart
223 {
224   createConfig
225   
226   echo -n "Starting tftpd..."
227   runTftp
228   echo "done"
229   echo -n "Starting dhcpd..."
230   runDhcp
231   echo "done"
232   echo -n "Starting nfs..."
233   startNfs
234   echo "done"
235 }
236
237 function actionStop
238 {
239   stopTftp
240   stopDhcp
241   stopNfs
242   notice "Terminal-server stopped"
243 }
244
245 function actionClean
246 {
247   actionStop
248   removeTftpConf
249   removeDhcpConf
250   stopNfs
251 }
252
253 function updateConfig
254 {
255   local service_="$1"
256
257   case "$service_" in
258     "") createConfig ;;
259     tftp) createTftpConf ;;
260     dhcp) createDhcpConf ;;
261     nfs) removeNfsConfig; createNfsConfig ;;
262     *) warn "Service $service_ not available" ;;
263   esac
264 }
265
266 # SERVICES {{{
267 function serviceStart
268 {
269   local service_="$1"   # service to start, if nothing => all
270
271   case "$service_" in
272     "") actionStart ;;
273     tftp) runTftp ;;
274     dhcp) runDhcp ;;
275     nfs) startNfs ;;
276     *) warn "Service $service_ not available" ;;
277   esac
278 }
279
280 function serviceStop
281 {
282   local service_="$1"   # service to stop, if nothing => all
283
284   case "$service_" in
285     "") actionStop ;;
286     tftp) stopTftp ;;
287     dhcp) stopDhcp ;;
288     nfs) stopNfs ;;
289     *) warn "Service $service_ not available" ;;
290   esac
291 }
292 # }}}
293
294 ###
295 ### __MAIN
296 ###
297
298 while getopts "fi:hv" opt; do
299   case "$opt" in
300     f) FORCE_='true' ;;
301     h) printUsage; exit ;;
302     v) let verbose_=$verbose_+1 ;;
303     ?) printUsage; exit 64 ;;
304   esac
305 done
306 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
307 setVerbose $verbose_
308
309 case "$1" in
310   help)   printUsage; exit 0 ;;
311 esac
312
313 checkRoot die "You have to be root to use this program"
314 disableSyslog
315
316 isExistent $DEFAULT_CONFIG_ die
317 . $DEFAULT_CONFIG_
318 . $CONFIG_
319 # used config vars:
320 # MOUNT_POINT_
321 # KERNEL_IMAGE_
322 # MEMTEST_IMAGE_
323 # PXE_BOOT_MSG_
324 # PXE_BOOT_LOGO_
325 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
326   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
327   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
328 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
329 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
330 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
331 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
332   die "False configuration, please update $CONFIG_"
333 fi
334   
335 case "$1" in
336   clean) actionClean; exit 0 ;;
337 esac
338
339
340 while true; do
341   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
342   if [ $? -eq 1 ]; then
343     $CONFIG_PROG_ && continue
344     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
345       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
346   else
347     break
348   fi  
349 done
350 source $CONF_FILE_
351
352 # check for necessary files
353 check_necessary_files_='no'
354 if [ "$1" == 'start' ]; then
355   case "$2" in
356     tftp|"") check_necessary_files_='yes' ;;
357   esac
358 fi
359 if [ $check_necessary_files_ == 'yes' ]; then
360   # test for files absolutly necessary for grml-terminalserver and created from -config
361   problem_=0
362   for i in $PATH_/minirt26.gz; do
363     isExistent $i warn || problem_=1
364   done
365   if [ $problem_ -eq 1 ]; then
366     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
367   fi
368 fi
369
370
371 case "$1" in
372   start) serviceStart "$2" ;;
373   stop) serviceStop "$2" ;;
374   config) updateConfig "$2" ;;
375   "")  actionStart ;;
376   *)  printUsage ;;
377 esac
378
379 # END OF FILE
380 ################################################################################
381 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2