-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathopenvpn-netns-shell
executable file
·201 lines (169 loc) · 4.77 KB
/
openvpn-netns-shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/sh
script_dir="$(dirname "`readlink -f "$0"`")"/openvpn-scripts
: ${NETNS:=vpn}
usage () {
prog="${0##*/}"
cat >&2 <<EOF
Start openvpn connection and run command in network namespace. When
the command terminates, openvpn connection is closed. Network
namespace name is given in the environment variable NETNS, or if it is
not set, the default name "vpn" is used.
usage:
$prog [<options>] <openvpn_options> ... (starts shell)
$prog [<options>] <openvpn_options> ... -- <command> [<args> ...]
Options for this program start with + to separate them from OpenVPN
options. They must be before any OpenVPN options.
+h, ++help show this help
+q, ++quiet don't show messages or OpenVPN output
+d, ++dbus start D-Bus session bus proxy
EOF
}
if [ $# -eq 0 ]; then
usage
exit 2
elif [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" = "--help" ]; then
usage
exit
fi
# handle our own options, which are first and start with + or ++
quiet=
netns_exec=netns-exec
_handle_option () {
opt=
case "$1" in
q|++quiet) quiet=true ;;
d|++dbus) netns_exec=netns-exec-dbus ;;
h|++help)
usage
exit
;;
'?')
echo "Unrecognized option: +$OPTARG" >&2
exit 2
;;
*)
echo "Unrecognized option: $1" >&2
exit 2
;;
esac
}
while [ $# -gt 0 -a "${1#+}" != "$1" ]; do
case "$1" in
++?*) # long option
_handle_option "$1"
;;
+?*) # one or more short options
OPTIND=1
while getopts :qdh opt -"${1#+}"; do
_handle_option "$opt"
done
;;
*)
echo "Unrecognized option: $1" >&2
exit 2
;;
esac
shift
done
# make fifo for signaling command termination
tmpdir=/dev/shm
[ -d "$tmpdir" ] || tmpdir=/tmp
fifo="$tmpdir/openvpn-netns-$$-fifo"
mkfifo "$fifo" || exit
# separate openvpn arguments into list of positional argument
# references for eval
openvpn_args_eval=
num_openvpn_args=0
_openvpn_args () {
while [ $# -gt 0 -a "$1" != "--" ]; do
num_openvpn_args=$(( num_openvpn_args + 1 ))
openvpn_args_eval="$openvpn_args_eval \"\${$num_openvpn_args}\""
shift
done
}
_openvpn_args "$@"
# Run the following as background job. Start openvpn as root, wait for
# command termination message, and then terminate openvpn. (sudo
# password prompt seems to work ok with this background-stdin-redirect
# mess.)
eval sudo /bin/sh -s '"$quiet" "$NETNS" "$script_dir"/netns "$fifo" "$tmpdir"' \
"$openvpn_args_eval" <<'EOF' &
quiet="$1"
netns="$2"
script="$3"
fifo="$4"
tmpdir="$5"
shift 5
IFS='
'
pidfile="$tmpdir/openvpn-netns-$$-openvpn.pid"
trap 'rm "$fifo" "$pidfile" 2>/dev/null' EXIT
trap 'exit 0' CHLD
openvpn \
--writepid "$pidfile" \
--ifconfig-noexec --route-noexec \
--script-security 2 \
--setenv NETNS "$netns" \
--up "$script" \
--route-up "$script" \
--down "$script" \
"$@" \
| \
while read -r line; do
[ -z "$quiet" ] && echo "openvpn: $line"
done &
# close stderr to suppress "interrupted system call" error
# messages from read if openvpn terminates on its own
exec 2>&-
# wait for command termination message from main script
read dummy_var < "$fifo"
# terminate openvpn
trap - CHLD
kill -TERM $(cat "$pidfile")
wait
EOF
sudo_pid=$!
# shift out openvpn arguments
shift $num_openvpn_args
# and "--" if any
[ $# -gt 0 ] && shift
# wait for netns creation
until [ -e "/var/run/netns/$NETNS" ]; do
sleep 1
# if the openvpn background job exited before creating the netns, exit
[ -e "/proc/$sudo_pid" ] || exit 125
done
# in the netns, wait for default IPv4 route, then run the command
$netns_exec -- "$NETNS" /bin/sh -c '
NETNS="$1"
quiet="$2"
shift 2
while [ -z "`ip route show to exact 0.0.0.0/0`" ]; do sleep 1; done
if [ -z "$quiet" ]; then
echo
echo "Connection established. Using netns \"$NETNS\"."
echo "Your IP address:" $(
ip -4 address show \
| sed -ne '\''s|^.*inet \([0-9]\+\(\.[0-9]\+\)\{3\}\)[^0-9].*$|\1|p'\'' \
| grep -v "^127\." )
echo "Starting $*"
echo
fi
if [ $# -gt 0 ]; then
exec "$@"
else
exec "${SHELL:-/bin/sh}"
fi
' '<wait-for-connection>' "$NETNS" "$quiet" "$@"
status=$?
if [ -z "$quiet" ]; then
echo
echo 'Command exited.'
fi
# send message to root subshell to terminate openvpn
echo done > "$fifo"
if [ -z "$quiet" ]; then
echo 'Waiting for openvpn to terminate.'
fi
wait
exit $status