grml-vnet: remove useless root check
[grml-network.git] / sbin / grml-vnet
1 #!/bin/bash
2 # Filename:      grml-vnet
3 # Purpose:       program to create tun/tap devices and add them to a bridge
4 # Authors:       Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 set -e
10 #set -x
11
12 PN_="`basename $0`"
13 OPT_DEL_='false'
14 OPT_USER_=''
15 OPT_GROUP_=''
16 OPT_BRIDGE_=''
17 OPT_AUTO_='false'
18
19
20 function printUsage()
21 {
22     cat <<EOT
23 Usage: "$PN_" [OPTIONS] <tun0> <tun1> ...
24
25 $PN_ creates persistent tun/tap devices with bridge handling
26
27 OPTIONS:
28    -d             delete the tun devices and remove them from the bridge if given
29    -u <user>      this user should be able to use the tun device
30    -g <group>     this group should be able to use the tun device
31    -b <bridge>    if given, all tun/tap devices are added/removed from the bridge
32    -a             enable auto mode, eg. create the bridge if not already existing and
33                   delete it when empty after removing given tun devices
34    -h             this help
35 EOT
36 }
37
38 function fromCmdline()
39 {
40     local action_="$1"
41     shift
42
43     while (( $# != 0 )); do
44         case "$1" in
45             "") continue ;;
46         esac
47         $action_ "$1"
48         shift
49     done
50 }
51
52 function createTun()
53 {
54     local args_=''
55     if [[ $OPT_USER_ != '' ]]; then args_="$args_ -u $OPT_USER_"; fi
56     if [[ $OPT_GROUP_ != '' ]]; then args_="$args_ -u $OPT_GROUP_"; fi
57     tunctl -t "$1" $args_
58     if [[ $OPT_BRIDGE_ != '' ]]; then
59         brctl addif "$OPT_BRIDGE_" "$1"
60     fi
61     ip link set up dev "$1"
62 }
63
64 function trashTun()
65 {
66     ip link set down dev "$1"
67     if [[ $OPT_BRIDGE_ != '' ]]; then
68         brctl delif "$OPT_BRIDGE_" "$1"
69     fi
70     tunctl -d "$1"
71 }
72
73 function die()
74 {
75     echo "$@" >&2
76     exit 1
77 }
78
79 ##
80 # MAIN
81 ##
82
83 while getopts "du:g:b:ah" opt; do
84     case "$opt" in
85         d) OPT_DEL_='true' ;;
86         u) OPT_USER_="$OPTARG" ;;
87         g) OPT_GROUP_="$OPTARG" ;;
88         b) OPT_BRIDGE_="$OPTARG" ;;
89         a) OPT_AUTO_='true' ;;
90         h) printUsage; exit 0 ;;
91         ?) printUsage; exit 1 ;;
92     esac
93 done
94 shift $(($OPTIND - 1))
95
96 if [[ $# < 1 ]]; then
97     echo "Error: Please give at least one device" >&2
98     printUsage
99     exit 1
100 fi
101
102 if [[ $OPT_DEL_ == 'false' ]]; then
103     if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
104         brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_"
105         ip link set up dev "$OPT_BRIDGE_"
106     fi
107     fromCmdline "createTun" "$@"
108 else
109     fromCmdline "trashTun" "$@"
110     if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
111         tmp_="`brctl showmacs "$OPT_BRIDGE_" |wc -l`"
112         if (( $tmp_ == 1 )); then
113             ip link set down dev "$OPT_BRIDGE_"
114             brctl delbr "$OPT_BRIDGE_"
115         else
116             die "E: bridge $OPT_BRIDGE_ not empty, not removing"
117         fi
118     fi
119 fi
120
121 ## END OF FILE #################################################################