2c62eca39a603655972ade29d68603b739f64022
[martins/ctdb.git] / config / events.d / 11.natgw
1 #!/bin/sh
2 # Script to set up one of the nodes as a NAT gateway for all other nodes.
3 # This is used to ensure that all nodes in the cluster can still originate
4 # traffic to the external network even if there are no public addresses
5 # available.
6 #
7
8 [ -n "$CTDB_BASE" ] || \
9     export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")
10
11 . $CTDB_BASE/functions
12 loadconfig
13
14 [ -n "$CTDB_NATGW_NODES" ] || exit 0
15 export CTDB_NATGW_NODES
16
17 set_natgw_capability ()
18 {
19     # Set NATGW capability depending on configuration
20     if [ "$CTDB_NATGW_SLAVE_ONLY" = "yes" ] ; then
21         ctdb setnatgwstate off
22     else
23         ctdb setnatgwstate on
24     fi
25 }
26
27 delete_all() {
28         _ip="${CTDB_NATGW_PUBLIC_IP%/*}"
29         _maskbits="${CTDB_NATGW_PUBLIC_IP#*/}"
30
31         [ -z "$CTDB_NATGW_PUBLIC_IFACE" ] || {
32             delete_ip_from_iface $CTDB_NATGW_PUBLIC_IFACE $_ip $_maskbits >/dev/null 2>&1
33         }
34         ip route del 0.0.0.0/0 metric 10 >/dev/null 2>/dev/null
35
36         # Delete the masquerading setup from a previous iteration where we
37         # were the NAT-GW
38         iptables -D POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK ! -d $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE >/dev/null 2>/dev/null
39
40         # remove any iptables rule we may have on this address
41         iptables -D INPUT -p tcp --syn -d $_ip/32 -j REJECT 2>/dev/null
42 }
43
44 ensure_natgwmaster ()
45 {
46     _event="$1"
47
48     set -- $(ctdb natgwlist)
49     natgwmaster="${1:--1}" # Default is -1 if natgwlist fails
50     natgwip="$2"
51
52     if [ "$natgwmaster" = "-1" ]; then
53         echo "There is no NATGW master node"
54         # The recovered event should never fail - we'll catch this
55         # failure in the monitor event.
56         if [ "$_event" = "recovered" ] ; then
57             exit 0
58         else
59             exit 1
60         fi
61     fi
62 }
63
64 case "$1" in 
65     setup)
66         set_natgw_capability
67         ;;
68
69     startup)
70         # Error if CTDB_NATGW_PUBLIC_IP is listed in public addresses
71         grep -q "^$CTDB_NATGW_PUBLIC_IP[[:space:]]" "${CTDB_PUBLIC_ADDRESSES:-/etc/ctdb/public_addresses}" && \
72             die "ERROR: NATGW configured to use a public address. NATGW must not use a public address."
73
74         # do not send out arp requests from loopback addresses
75         echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
76         ;;
77
78     recovered|updatenatgw|ipreallocated)
79         mypnn=$(ctdb pnn | cut -d: -f2)
80
81         set_natgw_capability
82         ensure_natgwmaster "$1"
83
84         delete_all
85
86         if [ "$mypnn" = "$natgwmaster" ]; then
87                 # This is the NAT GW
88                 echo 1 >/proc/sys/net/ipv4/ip_forward
89                 iptables -A POSTROUTING -t nat -s $CTDB_NATGW_PRIVATE_NETWORK ! -d $CTDB_NATGW_PRIVATE_NETWORK -j MASQUERADE
90
91                 # block all incoming connections to the natgw address
92                 ctdb_natgw_public_ip_host="${CTDB_NATGW_PUBLIC_IP%/*}/32"
93                 iptables -D INPUT -p tcp --syn -d $ctdb_natgw_public_ip_host -j REJECT 2>/dev/null
94                 iptables -I INPUT -p tcp --syn -d $ctdb_natgw_public_ip_host -j REJECT 2>/dev/null
95
96                 ip addr add $CTDB_NATGW_PUBLIC_IP dev $CTDB_NATGW_PUBLIC_IFACE
97                 ip route add 0.0.0.0/0 metric 10 via $CTDB_NATGW_DEFAULT_GATEWAY >/dev/null 2>/dev/null
98         else
99                 # This is NOT the NAT GW
100                 ip route add 0.0.0.0/0 via $natgwip metric 10
101                 # Make sure winbindd does not stay bound to this address
102                 # if we are no longer natgwmaster
103                 smbcontrol winbindd ip-dropped $CTDB_NATGW_PUBLIC_IP >/dev/null 2>/dev/null
104         fi
105
106         # flush our route cache
107         echo 1 > /proc/sys/net/ipv4/route/flush
108         ;;
109
110     shutdown|removenatgw)
111         delete_all
112         ;;
113
114     monitor)
115         set_natgw_capability
116         ensure_natgwmaster "$1"
117         ;;
118
119     *)
120         ctdb_standard_event_handler "@"
121         ;;
122 esac
123
124 exit 0