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