1 #!/bin/sh 2 # Usage: uml-net ( --start <username> | --stop ) 3 4 # Copyright (C) 2010, 2011, 2013 Paul Boddie <paul@boddie.org.uk> 5 # 6 # This program is free software; you can redistribute it and/or modify it under 7 # the terms of the GNU General Public License as published by the Free Software 8 # Foundation; either version 3 of the License, or (at your option) any later 9 # version. 10 # 11 # This program is distributed in the hope that it will be useful, but WITHOUT 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 # details. 15 # 16 # You should have received a copy of the GNU General Public License along with 17 # this program. If not, see <http://www.gnu.org/licenses/>. 18 19 # Configuration: 20 if [ -e userinstall-defaults-uml ]; then 21 . "$PWD/userinstall-defaults-uml" 22 elif [ -e /etc/default/userinstall-uml ]; then 23 . /etc/default/userinstall-uml 24 else 25 echo "No defaults found for userinstall (UML)." 1>&2 26 exit 1 27 fi 28 29 PROGNAME=`basename "$0"` 30 31 OPERATION=$1 32 USER=$2 33 34 if [ "$OPERATION" = "--start" ] && [ "$USER" ]; then 35 if ! chgrp adm /dev/net/tun ; then 36 echo "Cannot change group permission on tunnel device." 1>&2 37 exit 1 38 fi 39 if ! tunctl -u "$USER" -t "$TUNDEVICE" ; then 40 echo "Cannot configure a tunnel device for user $USER." 1>&2 41 exit 1 42 fi 43 if ! ifconfig "$TUNDEVICE" "$HOSTADDRESS" up ; then 44 echo "Could not start the tunnel device." 1>&2 45 exit 1 46 fi 47 sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" 48 iptables -t nat -I POSTROUTING -o "$NETDEVICE" -j MASQUERADE 49 iptables -I FORWARD -i "$TUNDEVICE" -j ACCEPT 50 iptables -I FORWARD -o "$TUNDEVICE" -j ACCEPT 51 52 elif [ "$OPERATION" = "--stop" ]; then 53 iptables -t nat -D POSTROUTING 1 54 iptables -D FORWARD 1 55 iptables -D FORWARD 1 56 tunctl -d "$TUNDEVICE" 57 else 58 echo "Usage: $PROGNAME --start <username> | --stop" 1>&2 59 exit 1 60 fi