macemu/BasiliskII/src/Unix/tunconfig
2016-05-28 02:20:35 +02:00

103 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
###########################################################################
# Configuration of the tunN devices for usage with Basilisk II.
# (derived MOL tunconfig script)
#
# This script should be named /usr/share/BasiliskII/tunconfig (unless
# the default name has been changed with the 'etherconfig' keyword).
#
# Usage: tunconfig iface up|down
#
# If the linux box is configured as a firewall, the rules below might
# need some adjustments.
#
# The IP Tunnel driver requires IP forwarding to be enabled. Run as root:
#
# echo 1 >/proc/sys/net/ipv4/ip_forward
#
###########################################################################
SUDO=/usr/bin/sudo
IFCONFIG=/sbin/ifconfig
IPTABLES=/sbin/iptables
#########################################################
[[ "x$1" = "x-n" ]] && {
DONT_EXECUTE=yes
shift 1
}
TUN_DEV=$1
ACTION=$2
TUN_NUM=`echo $TUN_DEV | sed s/[^0-9]//g`
NET_NUM=`expr 40 + $TUN_NUM`
TUN_NET=172.20.$NET_NUM.0/24
TUN_HOST=172.20.$NET_NUM.1
#########################################################
# Misc Checks
#########################################################
[[ $# = 2 ]] || {
echo "Usage: tunconfig [-n] iface up|down"
exit 2
}
[[ "`id -u`" = "0" ]] && {
echo "---> $SUDO not necessary." 1>&2
SUDO=""
}
[[ -x $IPTABLES ]] || {
echo "---> $IPTABLES not found." 1>&2
exit 1
}
if [ -n "$SUDO" ]; then
$SUDO -l | grep -q "NOPASSWD: $IFCONFIG" || {
echo "---> Missing sudo NOPASSWD: $IFCONFIG." 1>&2
exit 1
}
$SUDO -l | grep -q "NOPASSWD: $IPTABLES" || {
echo "---> Missing sudo NOPASSWD: $IPTABLES." 1>&2
exit 1
}
IFCONFIG="$SUDO $IFCONFIG"
IPTABLES="$SUDO $IPTABLES"
fi
[[ "x$DONT_EXECUTE" = "xyes" ]] && exit 0
$IPTABLES -L -n -t nat > /dev/null || exit 1
#########################################################
# Remove old (possibly stale) ruleset
#########################################################
{
$IPTABLES -t nat -D POSTROUTING -s $TUN_NET ! -d $TUN_NET -j MASQUERADE
} >& /dev/null
#########################################################
# Bring down interface
#########################################################
[[ "$ACTION" = down ]] && {
$IFCONFIG $TUN_DEV down
}
#########################################################
# Configure interface
#########################################################
[[ "$ACTION" = up ]] && {
$IFCONFIG $TUN_DEV $TUN_HOST
# masquerade the tun network
$IPTABLES -t nat -A POSTROUTING -s $TUN_NET ! -d $TUN_NET -j MASQUERADE
}
exit 0