chiark / gitweb /
* Use fd 8 for readconfig stdout parking rather than fd 10, to avoid
[autopkgtest.git] / xen / initscript
1 #!/bin/bash
2 set -e
3 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
4
5 ### BEGIN INIT INFO
6 # Provides: adtxenlvm
7 # Required-Start: $network $local_fs
8 # Required-Stop:
9 # Should-Start: $remote_fs
10 # Should-Stop: $remote_fs
11 # Default-Start: 2 3 4 5
12 # Default-Stop: 0 1 6
13 # Short-Description: Prepare firewall tables for autopkgtest Xen guests
14 ### END INIT INFO
15
16 lsbif=/lib/lsb/init-functions
17 if test -e $lsbif; then
18   . $lsbif
19 else
20   log_daemon_msg () { printf "%s: " "$1"; }
21   log_progress_msg () { printf "%s " "$1"; }
22   log_end_msg () { echo "done."; }
23 fi
24 . /etc/default/rcS
25
26 chains='AdtXenIn AdtXenFwd AdtXenIcmp'
27
28 if ! type iptables >/dev/null 2>&1 || ! type xm >/dev/null 2>&1; then
29   exit 0
30 fi
31
32 safety () {
33   log_progress_msg block
34   iptables -I INPUT -j DROP
35   iptables -I FORWARD -j DROP
36   trap '
37     for chain in $chains; do iptables -I $chain -j DROP; done
38     unsafety
39     exit 127
40   ' 0
41 }
42
43 unsafety () {
44   log_progress_msg unblock
45   iptables -D INPUT -j DROP
46   iptables -D FORWARD -j DROP
47   trap '' 0
48 }
49
50 case "$1" in
51 stop)
52   log_daemon_msg "adtxenlvm: removing firewall rules"
53   safety
54   log_progress_msg clear
55   for chain in $chains; do
56     if iptables -L -n $chain >/dev/null 2>&1; then
57       log_progress_msg $chain
58       iptables -F $chain
59       iptables -X $chain
60     fi
61   done
62   unsafety
63   log_end_msg 0
64   exit 0
65   ;;
66 start|restart|force-reload)
67   ;;
68 '')
69   echo >&2 "usage: /etc/init.d/adt-xen stop|start|restart|force-reload"
70   exit 1
71   ;;
72 *)
73   echo >&2 "init.d/adt-xen unsupported action $1"
74   exit 1
75   ;;
76 esac
77
78 set --
79
80 exec 8>&1
81 case "$VERBOSE" in
82 no)     exec >/dev/null ;;
83 esac
84
85 printf "adtxenlvm: reading configuration for firewall setup:\n"
86 . ${ADT_XENLVM_SHARE:=/usr/share/autopkgtest/xenlvm}/readconfig
87
88 exec >&8 8>&-
89
90 log_daemon_msg "adtxenlvm: installing firewall rules"
91
92 safety
93
94 log_progress_msg create
95 for chain in $chains; do
96   log_progress_msg $chain
97   iptables -N $chain >/dev/null 2>&1 || iptables -F $chain
98   iptables -I $chain -j DROP
99 done
100 unsafety
101
102 log_progress_msg rules
103
104 iptables -A AdtXenIcmp -j ACCEPT -p icmp --icmp-type echo-request
105 # per RFC1122, allow ICMP echo exchanges with anyone we can talk to at all
106
107 for oktype in                                   \
108         echo-reply                              \
109         destination-unreachable source-quench   \
110         time-exceeded parameter-problem         \
111 ;do
112   iptables -A AdtXenIcmp -j ACCEPT -m conntrack --ctstate ESTABLISHED \
113                 -p icmp --icmp-type  $oktype
114 done
115
116 main=AdtXenFwd
117
118 for i in $adt_fw_localmirrors; do
119   iptables -A $main -d $i -j ACCEPT -p tcp --dport 80
120   iptables -A $main -d $i -j AdtXenIcmp -p icmp
121 done
122
123 exec </etc/resolv.conf
124 while read command rest; do
125   if [ "x$command" = "xnameserver" ]; then
126     iptables -A $main -d $rest -j ACCEPT -p tcp --dport 53
127     iptables -A $main -d $rest -j ACCEPT -p udp --dport 53
128     iptables -A $main -d $rest -j AdtXenIcmp -p icmp
129   fi
130 done
131
132 for i in $adt_fw_testbedclients; do
133   iptables -A $main -d $i -j ACCEPT -p tcp ! --syn
134   iptables -A $main -d $i -j AdtXenIcmp -p icmp
135 done
136
137 for i in $adt_fw_prohibnets; do
138   iptables -A $main -d $i -j REJECT --reject-with icmp-net-prohibited
139 done
140
141 if [ x"$adt_fw_allowglobalports" != x ]; then
142   iptables -A $main -p icmp -j AdtXenIcmp
143 fi
144 for port in $adt_fw_allowglobalports; do
145   iptables -A $main -p tcp --dport $port -j ACCEPT
146 done
147
148 if [ "x$adt_fw_hook" != x ]; then
149   log_progress_msg hook
150   . $adt_fw_hook
151 fi
152
153 log_progress_msg confirm
154
155 iptables -A $main -j REJECT --reject-with icmp-admin-prohibited
156 iptables -D $main -j DROP
157
158 log_progress_msg engage
159
160 iptables -A AdtXenIn -j ACCEPT -p icmp --icmp-type echo-request
161 iptables -A AdtXenIn -j ACCEPT -m conntrack --ctstate ESTABLISHED
162 iptables -A AdtXenIn -j AdtXenFwd
163 iptables -D AdtXenIn -j DROP
164
165 iptables -D AdtXenIcmp -j DROP
166
167 log_progress_msg proxyarp
168
169 echo 1 >/proc/sys/net/ipv4/conf/eth0/proxy_arp 
170
171 log_end_msg 0