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 ################################################################################
15 #. /etc/grml/sysexits-sh
26 # this file holds all variable definitions
27 SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
28 isExistent $SHARED_PROG_VARS_ die
39 Usage: "$PROG_NAME__" [OPTIONS] <command>
41 $PROG_NAME__ is the config program for the terminalserver
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
55 ipt Iptables setup (snat for clients)
56 nfs All necessary nfs daemons
60 -v verbose (show what is going on, v++)
67 function killPortmapper
69 if [ -f /etc/init.d/portmap ] ; then
70 /etc/init.d/portmap stop >/dev/null &>/dev/null
71 elif [ -f /etc/init.d/rpcbind ] ; then
72 /etc/init.d/rpcbind stop >/dev/null &>/dev/null
74 killall -9 portmap &>/dev/null
75 killall -9 rpcbind &>/dev/null
79 function createDhcpConf
81 if [ -e "$DHCPD_CONFIG_FILE_" ]; then
82 if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
83 execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
84 execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
86 if [[ $FORCE_ == "true" ]]; then
87 execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
88 execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
90 warn "Not updating user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
94 execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
98 function removeDhcpConf
100 if [ -e "$DHCPD_CONFIG_FILE_" ]; then
101 if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
102 rm -f "$DHCPD_CONFIG_FILE_"
104 if [[ $FORCE_ == "true" ]]; then
105 rm -f "$DHCPD_CONFIG_FILE_"
107 warn "Not deleting user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
116 start-stop-daemon --stop --quiet --pidfile "$DHCPD_PID_"
118 # ugly but necessary :-/
119 find /var/lib/dhcp* -type f -name dhcpd.leases -delete
121 for dhcpdir in /var/lib/dhcp* ; do
122 touch ${dhcpdir}/dhcpd.leases
128 local conf_file_="$DHCPD_CONFIG_FILE_"
130 test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
131 start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
132 --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
137 isExistent "$DHCPD_CONFIG_FILE_" || \
138 warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
149 if [[ $IPTABLES_SNAT_ != "true" ]]; then
155 function startIptables
157 if [ -x /sbin/iptables ] ; then
158 if [[ $NAT_INTERFACE_ != "" ]]; then
159 local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
161 if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
162 echo "Rule for SNAT already present, nothing to be done."
164 echo "Setting up SNAT for terminalserver clients on ${NAT_INTERFACE_}:"
165 echo "* iptables -t nat -F POSTROUTING"
166 echo -n "* iptables -t nat -A POSTROUTING -o $NAT_INTERFACE_ -j SNAT --to-source $nat_source_ip_ ... "
167 { iptables -t nat -F POSTROUTING && \
168 iptables -t nat -A POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_" ; } && \
169 echo done || echo failed
171 if [ `cat /proc/sys/net/ipv4/ip_forward` -eq 1 ]; then
172 echo "IP-Forwarding already enabled, nothing to be done."
174 echo -n "Enabling IP-Forwarding: "
175 echo 1 > /proc/sys/net/ipv4/ip_forward && echo done || echo failed
179 warn "iptables executable not avilable"
183 function stopIptables
185 if [[ $IPTABLES_SNAT_ != "true" ]]; then
188 if [ -x /sbin/iptables ] ; then
189 if [[ $NAT_INTERFACE_ != "" ]]; then
190 local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
192 if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
193 iptables -t nat -F POSTROUTING &>/dev/null && \
194 iptables -t nat -D POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_"
196 echo 0 > /proc/sys/net/ipv4/ip_forward
203 function removeTftpConf
205 rm -rf $TFTPD_DATA_DIR_/*
207 function createTftpConf
211 execute "mkdir -p $TFTPD_DATA_DIR_/pxelinux.cfg" die
212 execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
213 execute "install -m 644 $PATH_/initrd.img $TFTPD_DATA_DIR_" die
214 if [ -d "$MOUNT_POINT_"/boot/release ] ; then
215 cp -r "$MOUNT_POINT_"/boot/release "$TFTPD_DATA_DIR_"
217 execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/vmlinuz" die
219 [ -f "$MEMTEST_IMAGE" ] && execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
220 execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
221 [ -f "$PXE_BOOT_LOGO_" ] && execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
223 execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
228 start-stop-daemon --stop --quiet -p "$TFTPD_PID_"
232 test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
233 start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- --daemon --no-multicast --pidfile "$TFTPD_PID_" --bind-address "$IP_" "$TFTPD_DATA_DIR_" || \
234 warn "problems starting tftpd server"
246 function createNfsConfig
248 execute "exportfs -o ro,no_root_squash,async,nohide,fsid=42 $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
251 function removeNfsConfig
253 execute "exportfs -u -o ro,no_root_squash,async,nohide,fsid=42 $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
258 if [ -f /etc/init.d/portmap ] ; then
259 /etc/init.d/portmap start
260 elif [ -f /etc/init.d/rpcbind ] ; then
261 /etc/init.d/rpcbind start
263 echo "Warning: Could not start portmapper/rpcbind" >&2
265 /etc/init.d/nfs-common start
266 # FIXME /etc/init.d/nfs-kernel-server start
267 $USR_SHARE_/nfs-kernel-server start
275 if [[ `exportfs |wc -l` > 0 ]]; then
276 dprint "There are other exports, not stopping NFS serivces"
278 /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
279 /etc/init.d/nfs-common stop >/dev/null 2>&1
286 function createConfig
289 execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
290 execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
300 echo -n "Starting tftpd: "
301 runTftp && echo done || echo failed
303 echo -n "Starting dhcpd: "
304 runDhcp && echo done || echo failed
308 echo "Finally starting nfs services..."
309 startNfs && echo "Successfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver failed!'
318 notice "Terminal-server stopped"
329 function updateConfig
335 tftp) createTftpConf ;;
336 dhcp) createDhcpConf ;;
337 nfs) removeNfsConfig; createNfsConfig ;;
338 *) warn "Service $service_ not available" ;;
343 function serviceStart
345 local service_="$1" # service to start, if nothing => all
351 ipt) startIptables ;;
353 *) warn "Service $service_ not available" ;;
359 local service_="$1" # service to stop, if nothing => all
367 *) warn "Service $service_ not available" ;;
376 while getopts "fi:hv" opt; do
379 h) printUsage; exit ;;
380 v) let verbose_=$verbose_+1 ;;
381 ?) printUsage; exit 64 ;;
384 shift $(($OPTIND - 1)) # set ARGV to the first not parsed commandline parameter
388 help) printUsage; exit 0 ;;
391 checkRoot die "You have to be root to use this program"
394 isExistent $DEFAULT_CONFIG_ die
403 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
404 $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
405 warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
406 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
407 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
408 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
409 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
410 die "False configuration, please update $CONFIG_"
414 clean) actionClean; exit 0 ;;
419 isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
420 if [ $? -eq 1 ]; then
421 $CONFIG_PROG_ && continue
422 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
423 "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
430 # check for necessary files
431 check_necessary_files_='no'
432 if [ "$1" == 'start' ]; then
434 tftp|"") check_necessary_files_='yes' ;;
437 if [ $check_necessary_files_ == 'yes' ]; then
438 # test for files absolutly necessary for grml-terminalserver and created from -config
440 for i in $PATH_/initrd.img; do
441 isExistent $i warn || problem_=1
443 if [ $problem_ -eq 1 ]; then
444 die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
450 start) serviceStart "$2" ;;
451 stop) serviceStop "$2" ;;
452 config) updateConfig "$2" ;;
458 ################################################################################
459 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2