forked from arthepsy/pan-globalprotect-okta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpnc-script-wrapper.sh
executable file
·112 lines (101 loc) · 2.41 KB
/
vpnc-script-wrapper.sh
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
#!/bin/sh
_VPNC_SCRIPT="/usr/local/sbin/vpnc-script"
# addr/mask/masklen
_ROUTES="
10.0.0.0/255.0.0.0/8
192.168.0.0/255.255.0.0/16
"
# zone [insecure]
_DOMAINS="
example.com 0
"
OIFS=$IFS; NLIFS='
'
_split() {
_line=$1; IFS=$2; shift 2
read -r -- "$@" <<-EOF
${_line}
EOF
}
_get_unbound() {
command -v /usr/local/sbin/unbound-control >/dev/null 2>&1 \
&& echo "/usr/local/sbin/unbound-control" && return 0
command -v unbound-control >/dev/null 2>&1 \
&& echo "unbound-control" && return 0
return 1
}
output_routes()
{
[ X"${reason}" != X"connect" ] && return
if [ -n "${CISCO_SPLIT_INC}" ]; then
_i=0
while [ ${_i} -lt ${CISCO_SPLIT_INC} ] ; do
eval _addr="\${CISCO_SPLIT_INC_${_i}_ADDR}"
eval _mask="\${CISCO_SPLIT_INC_${_i}_MASK}"
eval _masklen="\${CISCO_SPLIT_INC_${_i}_MASKLEN}"
echo "orig.route: ${_addr}/${_masklen}"
_i=$(expr ${_i} + 1)
done
fi
}
adjust_routes()
{
for _k in $(env | grep ^CISCO_SPLIT_INC | cut -d '=' -f 1); do
unset ${_k}
done
IFS=$NLIFS
_i=0;
for _line in ${_ROUTES}; do
_split "${_line}" ' ' _route _
_split "${_route}" '/' _addr _mask _masklen _
export CISCO_SPLIT_INC_${_i}_ADDR="${_addr}"
export CISCO_SPLIT_INC_${_i}_MASK="${_mask}"
export CISCO_SPLIT_INC_${_i}_MASKLEN="${_masklen}"
if [ X"${reason}" = X"connect" ]; then
echo "new.route: ${_addr}/${_masklen}"
fi
_i=$(expr ${_i} + 1)
done
export CISCO_SPLIT_INC=${_i}
IFS=$OIFS
}
output_dns()
{
[ X"${reason}" != X"connect" ] && return
echo "orig.dns: ${INTERNAL_IP4_DNS}"
echo "orig.domain: ${CISCO_DEF_DOMAIN}"
}
adjust_dns()
{
_dns="${INTERNAL_IP4_DNS}"
[ -z "${_dns}" ] && return
_unbound=$(_get_unbound) || \
{ echo "warn: unbound not found" >&2 && return; }
unset CISCO_DEF_DOMAIN
unset INTERNAL_IP4_DNS
IFS=$NLIFS
for _line in ${_DOMAINS}; do
_split "${_line}" ' ' _domain _insecure _
if [ X"${reason}" = X"connect" ]; then
if [ X"${_insecure}" = X"1" ]; then
${_unbound} insecure_add "${_domain}"
fi
${_unbound} forward_add +i "${_domain}" "${_dns}"
${_unbound} flush_requestlist
${_unbound} flush_zone "${_domain}"
elif [ X"${reason}" = X"disconnect" ]; then
if [ X"${_insecure}" = X"1" ]; then
${_unbound} insecure_remove "${_domain}"
fi
${_unbound} forward_remove +i "${_domain}"
${_unbound} flush_zone "${_domain}"
${_unbound} flush_requestlist
fi
done
IFS=$OIFS
}
output_routes
adjust_routes
output_dns
adjust_dns
${_VPNC_SCRIPT} $@