11#! /usr/bin/env bash
2-
3-
4-
52 #
63 # Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
74 #
1815 # You should have received a copy of the GNU General Public License
1916 # along with this program. If not, see <http://www.gnu.org/licenses/>.
2017 #
21-
22-
2318# $Id: firewall.sh 9947 2010-06-25 19:34:24Z manuel $ $HeadURL: svn://svn.lab.vmops.com/repos/vmdev/java/patches/xenserver/root/firewall.sh $
2419# firewall.sh -- allow some ports / protocols to vm instances
2520#
2621#
2722# @VERSION@
2823
2924usage () {
30- printf " Usage: %s: (-A|-D) -r <target-instance-ip> -P protocol (-p port_range | -t icmp_type_code) -l <public ip address> -d <target port> [-G] \n" $( basename $0 ) >&2
25+ printf " Usage: %s: (-A|-D) -r <target-instance-ip> -P protocol (-p port_range | -t icmp_type_code) -l <public ip address> -d <target port> -s <source cidrs> [-G] \n" $( basename $0 ) >&2
3126}
3227
33- set -x
28+ # set -x
3429
3530get_dev_list () {
3631 ip link show | grep -e eth[2-9] | awk -F " :" ' {print $2}'
@@ -57,6 +52,7 @@ tcp_or_udp_entry() {
5752 local port=$4
5853 local op=$5
5954 local proto=$6
55+ local cidrs=$7
6056 logger -t cloud " $( basename $0 ) : creating port fwd entry for PAT: public ip=$publicIp \
6157 instance ip=$instIp proto=$proto port=$port dport=$dport op=$op "
6258
@@ -73,11 +69,11 @@ tcp_or_udp_entry() {
7369 (sudo iptables -t nat $op OUTPUT --proto $proto -d $publicIp \
7470 --destination-port $port -j DNAT \
7571 --to-destination $instIp :$dport & >> $OUTFILE || [ " $op " == " -D" ]) &&
76- (sudo iptables $op FORWARD -p $proto -s 0/0 -d $instIp -m state \
72+ (sudo iptables $op FORWARD -p $proto -s $cidrs -d $instIp -m state \
7773 --state ESTABLISHED,RELATED -m comment --comment " $publicIp :$port " -j ACCEPT & >> $OUTFILE || [ " $op " == " -D" ]) &&
78- (sudo iptables $op FORWARD -p $proto -s 0/0 -d $instIp \
74+ (sudo iptables $op FORWARD -p $proto -s $cidrs -d $instIp \
7975 --destination-port $dport0 -m state --state NEW -m comment --comment " $publicIp :$port " -j ACCEPT & >> $OUTFILE )
80-
76+
8177
8278 local result=$?
8379 logger -t cloud " $( basename $0 ) : done port fwd entry for PAT: public ip=$publicIp op=$op result=$result "
@@ -100,10 +96,10 @@ icmp_entry() {
10096 # that the rules didn't exist in the first place
10197 local dev=$( ip_to_dev $publicIp )
10298 sudo iptables -t nat $op PREROUTING --proto icmp -i $dev -d $publicIp --icmp-type $icmptype -j DNAT --to-destination $instIp & >> $OUTFILE
103-
99+
104100 sudo iptables -t nat $op OUTPUT --proto icmp -d $publicIp --icmp-type $icmptype -j DNAT --to-destination $instIp & >> $OUTFILE
105101 sudo iptables $op FORWARD -p icmp -s 0/0 -d $instIp --icmp-type $icmptype -j ACCEPT & >> $OUTFILE
106-
102+
107103 result=$?
108104 logger -t cloud " $( basename $0 ) : done port fwd entry for PAT: public ip=$publicIp op=$op result=$result "
109105 return $result
@@ -150,39 +146,43 @@ pflag=
150146tflag=
151147lflag=
152148dflag=
149+ sflag=
153150Gflag=
154151op=" "
155152
156- while getopts ' ADr:P:p:t:l:d:G' OPTION
153+ while getopts ' ADr:P:p:t:l:d:s: G' OPTION
157154do
158155 case $OPTION in
159- A) op=" -A"
160- ;;
161- D) op=" -D"
162- ;;
163- r) rflag=1
164- instanceIp=" $OPTARG "
165- ;;
166- P) Pflag=1
167- protocol=" $OPTARG "
168- ;;
169- p) pflag=1
170- ports=" $OPTARG "
171- ;;
172- t) tflag=1
173- icmptype=" $OPTARG "
174- ;;
175- l) lflag=1
176- publicIp=" $OPTARG "
177- ;;
178- d) dflag=1
179- dport=" $OPTARG "
180- ;;
181- G) Gflag=1
182- ;;
183- ? ) usage
184- exit 2
185- ;;
156+ A) op=" -A"
157+ ;;
158+ D) op=" -D"
159+ ;;
160+ r) rflag=1
161+ instanceIp=" $OPTARG "
162+ ;;
163+ P) Pflag=1
164+ protocol=" $OPTARG "
165+ ;;
166+ p) pflag=1
167+ ports=" $OPTARG "
168+ ;;
169+ t) tflag=1
170+ icmptype=" $OPTARG "
171+ ;;
172+ l) lflag=1
173+ publicIp=" $OPTARG "
174+ ;;
175+ s) sflag=1
176+ cidrs=" $OPTARG "
177+ ;;
178+ d) dflag=1
179+ dport=" $OPTARG "
180+ ;;
181+ G) Gflag=1
182+ ;;
183+ ? ) usage
184+ exit 2
185+ ;;
186186 esac
187187done
188188
@@ -199,22 +199,27 @@ then
199199 exit $result
200200fi
201201
202+ if [ " $sflag " != " 1" ]
203+ then
204+ cidrs=" 0/0"
205+ fi
202206
203207case $protocol in
204- tcp|udp)
205- tcp_or_udp_entry $instanceIp $dport $publicIp $ports $op $protocol
208+ tcp|udp)
209+ tcp_or_udp_entry $instanceIp $dport $publicIp $ports $op $protocol $cidrs
206210 result=$?
207211 [ " $result " -ne 0 ] && cat $OUTFILE >&2
208212 rm -f $OUTFILE
209- exit $result
210- ;;
213+ exit $result
214+ ;;
211215 " icmp" )
212216
213- icmp_entry $instanceIp $icmptype $publicIp $op
214- exit $?
217+ icmp_entry $instanceIp $icmptype $publicIp $op
218+ exit $?
215219 ;;
216220 * )
217221 printf " Invalid protocol-- must be tcp, udp or icmp\n" >&2
218222 exit 5
219223 ;;
220224esac
225+
0 commit comments