Iterate over /var/lib/dhcp* directories for touch command.
[grml-terminalserver.git] / grml-terminalserver-config
1 #!/bin/bash
2 # Filename:      terminalserver-config
3 # Purpose:       configuration program for grml-terminalserver
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 ################################################################################
8
9
10 ###
11 ### __INCLUDES
12 ###
13 . /etc/grml/sh-lib
14 #. /etc/grml/sysexits-sh
15
16
17
18 ###
19 ### __VARIABLES
20 ###
21
22 verbose_=0
23
24 # this file holds all variable definitions
25 SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
26 isExistent $SHARED_PROG_VARS_ die
27 . $SHARED_PROG_VARS_
28
29 # variables used in the config file for grml-terminalserver
30 INTERFACE_=""
31 IP_=""
32 NETMASK_=""
33 GW_=""
34 NAMESERVERS_=""
35 IPRANGE_FROM_=""
36 IPRANGE_TO_=""
37 NETWORK_=""
38 OPTIONS_=""
39 BOOT_ARGS_=""
40 NAT_INTERFACE_=""
41
42
43 ###
44 ### __FUNCTIONS
45 ###
46
47 function printUsage
48 {
49   cat <<EOT
50 Usage: "$PROG_NAME__" [OPTIONS] <command>
51
52 $PROG_NAME__ is the config program for the terminalserver coming with grml.
53
54 COMMANDS:
55
56    help             This help text
57    interactive      Interactive Configuration of the grml-terminalserver
58    initrd           Only create the initrd
59    clean            Remove all configfiles created during user configuration
60    <default>        interactive mode
61
62 OPTIONS:
63    -v               verbose (show what is going on, v++)
64    -h               this help text
65
66 EOT
67 }
68
69
70 function writeConfig
71 {
72   local date_=""
73
74   if [ -f $CONF_FILE_ ]; then
75     mv -fb $CONF_FILE_ ${CONF_FILE_}-old
76   fi
77
78   date_=`date`
79   cat <<EOT > $CONF_FILE_
80 # GRML TERMINAL-SERVER CONFIG
81 # created on "$date_"
82 INTERFACE_="$INTERFACE_"
83 IP_="$IP_"
84 NETWORK_="$NETWORK_"
85 NETMASK_="$NETMASK_"
86 GW_="$GW_"
87 NAMESERVERS_="$NAMESERVERS_"
88 IPRANGE_FROM_="$IPRANGE_FROM_"
89 IPRANGE_TO_="$IPRANGE_TO_"
90 OPTIONS_="$OPTIONS_"
91 BOOT_ARGS_="$BOOT_ARGS_"
92 NAT_INTERFACE_="$NAT_INTERFACE_"
93
94 EOT
95   notice "config successfully safed to \"$CONF_FILE_\""
96 }
97
98
99 # AUTOMATIC CONFIGURATION  {{{
100 function checkParamArg
101 {
102   local param_name="$1"
103   local arg="$2"
104
105   #eval "echo $`echo $test`"
106   echo $arg |grep "^[-|+]" &>/dev/null || return
107
108   die "Argument from $param_name looks like another parameter \"$arg\"" 1
109 }
110
111 function actionAutoconf
112 {
113   checkParamArg "-i" "$interface_"
114 }
115 # }}}
116
117 # INITRD {{{
118 function actionMkInitrd
119 {
120   echo "Installing initrd $PATH_/minirt26.gz:"
121   # until we have a stable file location API let's use
122   # an according heuristic
123   for file in "$(find /live/image/boot -name initrd.gz)"; do
124     dir="$(dirname $file)"
125     if file "$dir"/linux26 | grep -q "$(uname -r)" ; then
126       cp -u "$dir"/initrd.gz "$PATH_"/minirt26.gz
127       echo '... done'
128       return 0
129     fi
130   done
131 }
132
133 # }}}
134
135 # INTERACTIVE CONFIGURATION  {{{
136
137 function actionInteractive
138 {
139   local i=""
140
141   dprint "running in interactive mode"
142
143   local card_title_="Choose network device connected to client network"
144   local card_message_="Available network devices:"
145   local iprange_title_="IP Address range for clients"
146 local iprange_message_="
147 Please enter the desired IP-Range of addresses that should be allocated by clients, separated by a single space.
148
149 Example:
150           192.168.0.101 192.168.0.200
151
152 for addresses from 192.168.0.101 to (and including) 192.168.0.200.
153
154 "
155   local runconfig_title_="Networkcard config"
156   local runconfig_message_="Would you like to configure your interfaces now?"
157
158   # on witch interfaces should we listen
159   local netdevices_="$(grep -ve 'lo:' -ve 'Inter-|' -ve 'face |bytes' /proc/net/dev | awk -F: '{print $1}')"
160   local device_list_=""
161   for INTERFACE_ in $netdevices_; do device_list_="$device_list_ ${INTERFACE_} Networkcard_${INTERFACE_##eth}"; done
162     echo -n "" >"$TMP_"
163     $DIALOG_ --backtitle "$BACK_TITLE_" --title "$card_title_" --menu "$card_message_" \
164       0 0 18 $device_list_ 2>"$TMP_" || warn "could not get network-interface"
165   INTERFACE_="$(<$TMP_)" ; echo -n "" >"$TMP_"
166
167   while true; do
168     IP_=`netGetIp "$INTERFACE_" warn`
169     NETMASK_=`netGetNetmask "$INTERFACE_" warn`
170     netValidIp "$IP_" warn && break
171     $DIALOG_ --backtitle "$BACK_TITLE_" --title "$runconfig_title_" --yesno "$runconfig_message_" 18 45 && \
172       netcardconfig || die "Could not get interface" $?
173   done
174
175   IPRANGE_FROM_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMin/{print $2}'`
176   IPRANGE_TO_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMax/{print $2}'`
177   NETWORK_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/Network:/{print $2}'`
178   NETWORK_=${NETWORK_%/*}
179   local iprange_=""
180   while [ -z "$IPRANGE_FROM_" -o -z "$IPRANGE_TO_" -o -z "$iprange_" ]; do
181     iprange_="$IPRANGE_FROM_ $IPRANGE_TO_"
182     echo -n "" >"$TMP_"
183     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$iprange_title_ ($INTERFACE_=$IP_/$NETMASK_)" \
184       --inputbox "$iprange_message_" 18 75 "$iprange_" 2>"$TMP_" || die "problems getting network range" $?
185
186     iprange_="$(<$TMP_)"
187     IPRANGE_FROM_="${iprange_%% *}"
188     IPRANGE_TO_="${iprange_##* }"
189
190     for i in "$IPRANGE_FROM_" "$IPRANGE_TO_"; do
191       netValidIp "$i" warn || iprange_=""
192     done
193   done
194
195   NAMESERVERS_=`netGetNameservers warn`
196   GW_=`netGetDefaultGateway warn`
197   GW_DEV_=`/sbin/ip route get "$GW_" | awk '{ print $3; exit; }'`
198   if [ "$GW_DEV_" != "$INTERFACE_" ] && [ "$GW_DEV_" != "" ]; then
199     # GW_DEV_ of server is not the same device as the one serviced by dhcpd
200     # so it doesn't make sense to provide the GW_ address to the clients
201     local do_nat_="YES"
202     local do_nat_title_="Network Address Translation"
203     local do_nat_message_="
204 Do you want to set up NAT so that clients booting from this
205 grml-terminalserver can use this machine also as gateway to
206 the internet?"
207
208     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$do_nat_title_" --yesno "$do_nat_message_" 15 75 || \
209       do_nat_="NO"
210     if [ "$do_nat_" = "YES" ]; then
211       # user wants NAT, we give the clients the server address as
212       # gateway as well
213       GW_="$IP_"
214       NAT_INTERFACE_="$GW_DEV_"
215     else
216       # no NAT, no sensible gateway
217       GW_=""
218       NAT_INTERFACE_=""
219     fi
220   fi
221
222   # get options
223   #local OPT_IPTABLES_="yes"
224   #local OPT_SSH_="yes"
225   #local OPT_DISTCC_="yes"
226   #local OPT_SQUID_=""
227
228   local OPTIONS_TITLE_="Options"
229   local OPTIONS_MESSAGE_="Please give the appropriate options you want the clients to use:
230
231 grml2hd   - Make a non-interactive remote installation
232
233
234
235 "
236 #  local OPTIONS_MESSAGE_="Please give the appropriate options you want the clients to use:
237 #
238 #iptables  - Only the server should be able to access the clients
239 #ssh       - A ssh-key will be created on the server and distributed to the clients
240 #distcc    - You want to use the clients as compile-farm (ssh options recommned)
241 #
242 #"
243
244   local OPT_IPTABLES_DESC_="Start iptables on the clients"
245   local OPT_SSH_DESC_="Start ssh on the clients"
246   local OPT_DISTCC_DESC_="Start distcc on the clients"
247   local OPT_GRML2HD_DESC_="Remote install grml on the network clients"
248   # dialog options (enable if implemented)
249   #iptables "$OPT_IPTABLES_DESC_" off \
250   #ssh "$OPT_SSH_DESC_" off \
251   #distcc "$OPT_DISTCC_DESC_" off \
252   echo -n "" >"$TMP_"
253   $DIALOG_ --clear --separate-output --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --checklist "$OPTIONS_MESSAGE_" 25 80 10 \
254   grml2hd "$OPT_GRML2HD_DESC_" off \
255     2>$TMP_ || die "could not get terminalserver options" $?
256   while read tmp_option_; do
257     OPTIONS_="$OPTIONS_ $tmp_option_"
258   done <$TMP_
259
260   # parse options
261   for i in $OPTIONS_; do
262     case "$i" in
263       grml2hd)  optGrml2Hd || return 1 ;;
264     esac
265   done
266
267   echo -n "" >"$TMP_"
268   local OPTIONS_BOOTARG_MESSAGE_="Here you can add additional boot arguments for the clients seperated by spaces:
269
270 Quite usefull examples:
271
272 ssh=<pw>              - Start ssh server and set password of user grml to pw
273 services=<1,2,3>      - Execute /etc/init.d/{1,2,3} start
274 console=ttyS0,9600n8  - Initialise serial console
275 startx                - Boot into X
276
277
278 "
279   $DIALOG_ --clear --no-collapse --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --inputbox "$OPTIONS_BOOTARG_MESSAGE_" 0 0\
280     2>$TMP_ || die "problems getting additional boot arguments"
281   BOOT_ARGS_="$BOOT_ARGS_ $(<$TMP_)"
282
283   writeConfig
284   actionMkInitrd
285   notice "GRML terminalserver successfully configured"
286 }
287 # }}}
288
289 # OPTIONS GETTING DIALOG {{{
290 function optGrml2Hd
291 {
292   local GRML2HD_TITLE_='Grml2hd options dialog'
293   local tmp_=''
294   local options_='BOOT_IMAGE=grml2hd'
295
296   # get partition to install grml2hd on
297   OPTIONS_PARTITION_MSG_='Please specify the target partition where to install grml'
298   PARTITION_TITLE_='Partition selection'
299   echo -n "" >"$TMP_"
300   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$PARTITION_TITLE_" --inputbox \
301     "$OPTIONS_PARTITION_MSG_" 0 75 '/dev/hda1' 2>$TMP_ || die "problems getting partition"
302   tmp_="partition=$(<$TMP_)"
303   options_="$options_ $tmp_"
304
305   # get filesystem type
306   OPTION_FS_TYPE_='Please specify the filesystem type'
307   FS_TITLE_='Filesystem selection'
308   echo -n "" >"$TMP_"
309   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$FS_TITLE_" --inputbox \
310   "$OPTION_FS_TYPE_" 0 75 'ext3' 2>$TMP_ || die "problems getting filesystem type"
311   tmp_="filesystem=$(<$TMP_)"
312   options_="$options_ $tmp_"
313
314   # get where to save mbr
315   OPTION_MBR_='Please specify the location where to save the mbr'
316   MBR_TITLE_='Select location of mbr'
317   echo -n "" >"$TMP_"
318   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$MBR_TITLE_" --inputbox \
319     "$OPTION_MBR_" 0 75 '/dev/hda' 2>$TMP_ || die "problems getting location where to write mbr"
320   tmp_="mbr=$(<$TMP_)"
321   options_="$options_ $tmp_"
322
323   # get first user
324   OPTION_USER_='Who should be the first user on the system'
325   USER_TITLE_='User selection'
326   echo -n "" >"$TMP_"
327   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$USER_TITLE_" --inputbox \
328     "$OPTION_USER_" 0 75 'grml' 2>$TMP_ || die "problems getting first user of system"
329   tmp_="user=$(<$TMP_)"
330   options_="$options_ $tmp_"
331
332   BOOT_ARGS_="$options_"
333 }
334 #}}}
335
336
337 function removeTmpFiles
338 {
339   execute "rm -f $TMP_" warn
340 }
341
342 function actionClean
343 {
344   for i in dhcpd.conf minirt26.gz; do
345     execute "rm -f $PATH_/$i*"
346   done
347
348   for i in $CARDS_DETECTED_BY_DISCOVER $CONF_FILE_; do
349     execute "rm -f $i"
350   done
351
352 }
353
354
355 ###
356 ### __MAIN
357 ###
358
359 while getopts "i:hv" opt; do
360   case "$opt" in
361     i) interface_=$OPTARG ;;
362     h) printUsage; exit ;;
363     v) let verbose_=$verbose_+1 ;;
364     ?) printUsage; exit 64 ;;
365   esac
366 done
367 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
368 setVerbose $verbose_
369
370 case "$1" in
371   help)   printUsage; exit 0 ;;
372 esac
373
374 checkRoot die 'You have to be root to use this program'
375 disableSyslog
376
377 execute "mkdir -p $PATH_" die
378
379 TMP_=`mktemp -t grml-terminalserver-config.XXXXXX` || die "Could not create tmpfile" $?
380 setExitFunction 'removeTmpFiles'
381
382
383 . $DEFAULT_CONFIG_
384 . $CONFIG_
385 # used config vars:
386 # MODULES_PATH_
387 # MODULES_PATH_ROOT_DIFF_
388 # KERNEL_VERSION_
389 # ORIGINAL_INITRD_
390 if [[ $MODULES_PATH_ == "" || $KERNEL_VERSION_ == "" || $ORIGINAL_INITRD_ == "" ]]; then
391   warn "MODULES_PATH_=\"$MODULES_PATH_\" \
392 KERNEL_VERSION_=\"$KERNEL_VERSION_\" \
393 ORIGINAL_INITRD_=\"$ORIGINAL_INITRD_\""
394   die "False configuration, please update $CONFIG_"
395 fi
396
397
398 case "$1" in
399   interactive)  actionInteractive ;;
400   initrd) actionMkInitrd ;;
401   clean)  actionClean ;;
402   *)    actionInteractive ;;
403 esac
404
405 removeTmpFiles
406 # END OF FILE
407 ################################################################################
408 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2