grml-addtun: added -a for automatic bridge handling
[grml-network.git] / sbin / grml-addtun
1 #!/bin/bash
2 # Filename:      grml-addtun
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 given 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 allready 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 }
62
63 function trashTun()
64 {
65     if [[ $OPT_BRIDGE_ != '' ]]; then
66         brctl delif "$OPT_BRIDGE_" "$1"
67     fi
68     tunctl -d "$1"
69 }
70
71 function die()
72 {
73     echo "$@" >&2
74     exit 1
75 }
76
77 ##
78 # MAIN
79 ##
80
81 while getopts "du:g:b:ah" opt; do
82     case "$opt" in
83         d) OPT_DEL_='true' ;;
84         u) OPT_USER_="$OPTARG" ;;
85         g) OPT_GROUP_="$OPTARG" ;;
86         b) OPT_BRIDGE_="$OPTARG" ;;
87         a) OPT_AUTO_='true' ;;
88         h) printUsage; exit 0 ;;
89         ?) printUsage; exit 1 ;;
90     esac
91 done
92 shift $(($OPTIND - 1))
93
94 if [[ $OPT_DEL_ == 'false' ]]; then
95     if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
96         brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_"
97     fi
98     fromCmdline "createTun" "$@"
99 else
100     fromCmdline "trashTun" "$@"
101     if [[ $OPT_BRIDGE_ != '' && $OPT_AUTO_ == 'true' ]]; then
102         tmp_="`brctl showmacs "$OPT_BRIDGE_" |wc -l`"
103         if (( $tmp_ == 1 )); then
104             brctl delbr "$OPT_BRIDGE_"
105         else
106             die "E: bridge $OPT_BRIDGE_ not empty, not removing"
107         fi
108     fi
109 fi
110
111 # vim: filetype=sh