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 /etc/init.d/portmap stop >/dev/null &>/dev/null
70 killall -9 portmap &>/dev/null
74 function createDhcpConf
76 if [ -e "$DHCPD_CONFIG_FILE_" ]; then
77 if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
78 execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
79 execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
81 if [[ $FORCE_ == "true" ]]; then
82 execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
83 execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
85 warn "Not updating user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
89 execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
93 function removeDhcpConf
95 if [ -e "$DHCPD_CONFIG_FILE_" ]; then
96 if grep $CONFIG_PATTERN_ $DHCPD_CONFIG_FILE_ &>/dev/null; then
97 rm -f "$DHCPD_CONFIG_FILE_"
99 if [[ $FORCE_ == "true" ]]; then
100 rm -f "$DHCPD_CONFIG_FILE_"
102 warn "Not deleting user edited configfile $DHCPD_CONFIG_FILE_, user -f to override"
111 start-stop-daemon --stop --quiet --pidfile "$DHCPD_PID_"
113 rm -f /var/lib/dhcp3/dhcpd.leases* 2>/dev/null #FIXME
114 touch /var/lib/dhcp3/dhcpd.leases
119 local conf_file_="$DHCPD_CONFIG_FILE_"
121 test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
122 start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
123 --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
128 isExistent "$DHCPD_CONFIG_FILE_" || \
129 warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
140 if [[ $IPTABLES_SNAT_ != "true" ]]; then
146 function startIptables
148 if [ -x /sbin/iptables ] ; then
149 if [[ $NAT_INTERFACE_ != "" ]]; then
150 local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
152 if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
153 echo "Rule for SNAT already present, nothing to be done."
155 echo "Setting up SNAT for terminalserver clients on ${NAT_INTERFACE_}:"
156 echo "* iptables -t nat -F POSTROUTING"
157 echo -n "* iptables -t nat -A POSTROUTING -o $NAT_INTERFACE_ -j SNAT --to-source $nat_source_ip_ ... "
158 { iptables -t nat -F POSTROUTING && \
159 iptables -t nat -A POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_" ; } && \
160 echo done || echo failed
162 if [ `cat /proc/sys/net/ipv4/ip_forward` -eq 1 ]; then
163 echo "IP-Forwarding already enabled, nothing to be done."
165 echo -n "Enabling IP-Forwarding: "
166 echo 1 > /proc/sys/net/ipv4/ip_forward && echo done || echo failed
170 warn "iptables executable not avilable"
174 function stopIptables
176 if [[ $IPTABLES_SNAT_ != "true" ]]; then
179 if [ -x /sbin/iptables ] ; then
180 if [[ $NAT_INTERFACE_ != "" ]]; then
181 local nat_source_ip_=`netGetIp "$NAT_INTERFACE_" warn`
183 if iptables -t nat -vnL POSTROUTING | grep -q "SNAT.*${NAT_INTERFACE_}.*to:${nat_source_ip_}" ; then
184 iptables -t nat -F POSTROUTING &>/dev/null && \
185 iptables -t nat -D POSTROUTING -o "$NAT_INTERFACE_" -j SNAT --to-source "$nat_source_ip_"
187 echo 0 > /proc/sys/net/ipv4/ip_forward
194 function removeTftpConf
196 rm -rf $TFTPD_DATA_DIR_/*
198 function createTftpConf
202 execute "mkdir -p $TFTPD_DATA_DIR_/pxelinux.cfg" die
203 execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
204 execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
205 execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
206 execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
207 execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
208 execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
210 execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
215 start-stop-daemon --stop --quiet -p "$TFTPD_PID_"
219 test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
220 start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- --daemon --no-multicast --pidfile "$TFTPD_PID_" --bind-address "$IP_" "$TFTPD_DATA_DIR_" || \
221 warn "problems starting tftpd server"
233 function createNfsConfig
235 execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
238 function removeNfsConfig
240 execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
245 /etc/init.d/portmap start
246 /etc/init.d/nfs-common start
247 # FIXME /etc/init.d/nfs-kernel-server start
248 $USR_SHARE_/nfs-kernel-server start
256 if [[ `exportfs |wc -l` > 0 ]]; then
257 dprint "There are other exports, not stopping NFS serivces"
259 /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
260 /etc/init.d/nfs-common stop >/dev/null 2>&1
267 function createConfig
270 execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
271 execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
281 echo -n "Starting tftpd: "
282 runTftp && echo done || echo failed
284 echo -n "Starting dhcpd: "
285 runDhcp && echo done || echo failed
289 echo "Finally starting nfs services..."
290 startNfs && echo "Successfully finished startup of grml-terminalserver." || echo 'Startup of grml-terminalserver failed!'
299 notice "Terminal-server stopped"
310 function updateConfig
316 tftp) createTftpConf ;;
317 dhcp) createDhcpConf ;;
318 nfs) removeNfsConfig; createNfsConfig ;;
319 *) warn "Service $service_ not available" ;;
324 function serviceStart
326 local service_="$1" # service to start, if nothing => all
332 ipt) startIptables ;;
334 *) warn "Service $service_ not available" ;;
340 local service_="$1" # service to stop, if nothing => all
348 *) warn "Service $service_ not available" ;;
357 while getopts "fi:hv" opt; do
360 h) printUsage; exit ;;
361 v) let verbose_=$verbose_+1 ;;
362 ?) printUsage; exit 64 ;;
365 shift $(($OPTIND - 1)) # set ARGV to the first not parsed commandline parameter
369 help) printUsage; exit 0 ;;
372 checkRoot die "You have to be root to use this program"
375 isExistent $DEFAULT_CONFIG_ die
384 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
385 $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
386 warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
387 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
388 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
389 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
390 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
391 die "False configuration, please update $CONFIG_"
395 clean) actionClean; exit 0 ;;
400 isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
401 if [ $? -eq 1 ]; then
402 $CONFIG_PROG_ && continue
403 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
404 "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
411 # check for necessary files
412 check_necessary_files_='no'
413 if [ "$1" == 'start' ]; then
415 tftp|"") check_necessary_files_='yes' ;;
418 if [ $check_necessary_files_ == 'yes' ]; then
419 # test for files absolutly necessary for grml-terminalserver and created from -config
421 for i in $PATH_/minirt26.gz; do
422 isExistent $i warn || problem_=1
424 if [ $problem_ -eq 1 ]; then
425 die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
431 start) serviceStart "$2" ;;
432 stop) serviceStop "$2" ;;
433 config) updateConfig "$2" ;;
439 ################################################################################
440 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2