Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | SHELL="/bin/bash" |
| 4 | |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 5 | NIC=$( route|grep default|awk '{print $NF}' ) |
Andy Bavier | 8cd2b78 | 2016-02-15 10:46:09 -0500 | [diff] [blame] | 6 | PORTAL=$( dig +short portal.opencloud.us | tail -1 ) |
Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 7 | |
| 8 | NAME="${1}" |
| 9 | OP="${2}" |
| 10 | SUBOP="${3}" |
| 11 | ARGS="${4}" |
| 12 | |
| 13 | add_rule() { |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 14 | CHAIN=$1 |
| 15 | ARGS=$2 |
| 16 | iptables -C $CHAIN $ARGS |
Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 17 | if [ "$?" -ne 0 ] |
| 18 | then |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 19 | iptables -I $CHAIN 1 $ARGS |
Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 20 | fi |
| 21 | } |
| 22 | |
| 23 | add_local_access_rules() { |
| 24 | SUBNET=$( ip addr show $NIC|grep "inet "|awk '{print $2}' ) |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 25 | PRIVATENET=$( ip addr show virbr0|grep "inet "|awk '{print $2}' ) |
| 26 | add_rule "FORWARD" "-s $SUBNET -j ACCEPT" |
| 27 | # Don't NAT traffic from service VMs destined to the local subnet |
| 28 | add_rule "POSTROUTING" "-t nat -s $PRIVATENET -d $SUBNET -j RETURN" |
Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | add_portal_access_rules() { |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 32 | add_rule "FORWARD" "-s $PORTAL -j ACCEPT" |
Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | add_web_access_rules() { |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 36 | add_rule "FORWARD" "-p tcp --dport 80 -j ACCEPT" |
Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | if [ "$OP" = "start" ] |
| 40 | then |
| 41 | add_local_access_rules |
| 42 | add_portal_access_rules |
| 43 | add_web_access_rules |
Andy Bavier | cc27db0 | 2015-10-05 15:02:47 -0400 | [diff] [blame] | 44 | fi |