Merge to iptables-1.3.5
[iptables.git] / iptables.init
1 #!/bin/sh
2 #
3 # iptables      Start iptables firewall
4 #
5 # chkconfig: 2345 08 92
6 # description:  Starts, stops and saves iptables firewall
7 #
8 # config: /etc/sysconfig/iptables
9 # config: /etc/sysconfig/iptables-config
10
11 # Source function library.
12 . /etc/init.d/functions
13
14 IPTABLES=iptables
15 IPTABLES_DATA=/etc/sysconfig/$IPTABLES
16 IPTABLES_CONFIG=/etc/sysconfig/${IPTABLES}-config
17 IPV=${IPTABLES%tables} # ip for ipv4 | ip6 for ipv6
18 PROC_IPTABLES_NAMES=/proc/net/${IPV}_tables_names
19 VAR_SUBSYS_IPTABLES=/var/lock/subsys/$IPTABLES
20
21 if [ ! -x /sbin/$IPTABLES ]; then
22     echo -n $"/sbin/$IPTABLES does not exist."; warning; echo
23     exit 0
24 fi
25
26 if lsmod 2>/dev/null | grep -q ipchains ; then
27     echo -n $"ipchains and $IPTABLES can not be used together."; warning; echo
28     exit 0
29 fi
30
31 # Old or new modutils
32 /sbin/modprobe --version 2>&1 | grep -q module-init-tools \
33     && NEW_MODUTILS=1 \
34     || NEW_MODUTILS=0
35
36 # Default firewall configuration:
37 IPTABLES_MODULES=""
38 IPTABLES_MODULES_UNLOAD="yes"
39 IPTABLES_SAVE_ON_STOP="no"
40 IPTABLES_SAVE_ON_RESTART="no"
41 IPTABLES_SAVE_COUNTER="no"
42 IPTABLES_STATUS_NUMERIC="no"
43
44 # Load firewall configuration.
45 [ -f "$IPTABLES_CONFIG" ] && . "$IPTABLES_CONFIG"
46
47 rmmod_r() {
48     # Unload module with all referring modules.
49     # At first all referring modules will be unloaded, then the module itself.
50     local mod=$1
51     local ret=0
52     local ref=
53
54     # Get referring modules.
55     # New modutils have another output format.
56     [ $NEW_MODUTILS = 1 ] \
57         && ref=`lsmod | awk "/^${mod}/ { print \\\$4; }" | tr ',' ' '` \
58         || ref=`lsmod | grep ^${mod} | cut -d "[" -s -f 2 | cut -d "]" -s -f 1`
59
60     # recursive call for all referring modules
61     for i in $ref; do
62         rmmod_r $i
63         let ret+=$?;
64     done
65
66     # Unload module.
67     # The extra test is for 2.6: The module might have autocleaned,
68     # after all referring modules are unloaded.
69     if grep -q "^${mod}" /proc/modules ; then
70         modprobe -r $mod > /dev/null 2>&1
71         let ret+=$?;
72     fi
73
74     return $ret
75 }
76
77 flush_n_delete() {
78     # Flush firewall rules and delete chains.
79     [ -e "$PROC_IPTABLES_NAMES" ] || return 1
80
81     # Check if firewall is configured (has tables)
82     tables=`cat $PROC_IPTABLES_NAMES 2>/dev/null`
83     [ -z "$tables" ] && return 1
84
85     echo -n $"Flushing firewall rules: "
86     ret=0
87     # For all tables
88     for i in $tables; do
89         # Flush firewall rules.
90         $IPTABLES -t $i -F;
91         let ret+=$?;
92
93         # Delete firewall chains.
94         $IPTABLES -t $i -X;
95         let ret+=$?;
96
97         # Set counter to zero.
98         $IPTABLES -t $i -Z;
99         let ret+=$?;
100     done
101
102     [ $ret -eq 0 ] && success || failure
103     echo
104     return $ret
105 }
106
107 set_policy() {
108     # Set policy for configured tables.
109     policy=$1
110
111     # Check if iptable module is loaded
112     [ ! -e "$PROC_IPTABLES_NAMES" ] && return 1
113
114     # Check if firewall is configured (has tables)
115     tables=`cat $PROC_IPTABLES_NAMES 2>/dev/null`
116     [ -z "$tables" ] && return 1
117
118     echo -n $"Setting chains to policy $policy: "
119     ret=0
120     for i in $tables; do
121         echo -n "$i "
122         case "$i" in
123             filter)
124                 $IPTABLES -t filter -P INPUT $policy \
125                     && $IPTABLES -t filter -P OUTPUT $policy \
126                     && $IPTABLES -t filter -P FORWARD $policy \
127                     || let ret+=1
128                 ;;
129             nat)
130                 $IPTABLES -t nat -P PREROUTING $policy \
131                     && $IPTABLES -t nat -P POSTROUTING $policy \
132                     && $IPTABLES -t nat -P OUTPUT $policy \
133                     || let ret+=1
134                 ;;
135             mangle)
136                 $IPTABLES -t mangle -P PREROUTING $policy \
137                     && $IPTABLES -t mangle -P POSTROUTING $policy \
138                     && $IPTABLES -t mangle -P INPUT $policy \
139                     && $IPTABLES -t mangle -P OUTPUT $policy \
140                     && $IPTABLES -t mangle -P FORWARD $policy \
141                     || let ret+=1
142                 ;;
143             *)
144                 let ret+=1
145                 ;;
146         esac
147     done
148
149     [ $ret -eq 0 ] && success || failure
150     echo
151     return $ret
152 }
153
154 start() {
155     # Do not start if there is no config file.
156     [ -f "$IPTABLES_DATA" ] || return 1
157
158     echo -n $"Applying $IPTABLES firewall rules: "
159
160     OPT=
161     [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
162
163     $IPTABLES-restore $OPT $IPTABLES_DATA
164     if [ $? -eq 0 ]; then
165         success; echo
166     else
167         failure; echo; return 1
168     fi
169     
170     # Load additional modules (helpers)
171     if [ -n "$IPTABLES_MODULES" ]; then
172         echo -n $"Loading additional $IPTABLES modules: "
173         ret=0
174         for mod in $IPTABLES_MODULES; do
175             echo -n "$mod "
176             modprobe $mod > /dev/null 2>&1
177             let ret+=$?;
178         done
179         [ $ret -eq 0 ] && success || failure
180         echo
181     fi
182     
183     touch $VAR_SUBSYS_IPTABLES
184     return $ret
185 }
186
187 stop() {
188     # Do not stop if iptables module is not loaded.
189     [ -e "$PROC_IPTABLES_NAMES" ] || return 1
190
191     flush_n_delete
192     set_policy ACCEPT
193     
194     if [ "x$IPTABLES_MODULES_UNLOAD" = "xyes" ]; then
195         echo -n $"Unloading $IPTABLES modules: "
196         ret=0
197         rmmod_r ${IPV}_tables
198         let ret+=$?;
199         rmmod_r ${IPV}_conntrack
200         let ret+=$?;
201         [ $ret -eq 0 ] && success || failure
202         echo
203     fi
204     
205     rm -f $VAR_SUBSYS_IPTABLES
206     return $ret
207 }
208
209 save() {
210     # Check if iptable module is loaded
211     [ ! -e "$PROC_IPTABLES_NAMES" ] && return 1
212
213     # Check if firewall is configured (has tables)
214     tables=`cat $PROC_IPTABLES_NAMES 2>/dev/null`
215     [ -z "$tables" ] && return 1
216
217     echo -n $"Saving firewall rules to $IPTABLES_DATA: "
218
219     OPT=
220     [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"
221
222     ret=0
223     TMP_FILE=`/bin/mktemp -q /tmp/$IPTABLES.XXXXXX` \
224         && chmod 600 "$TMP_FILE" \
225         && $IPTABLES-save $OPT > $TMP_FILE 2>/dev/null \
226         && size=`stat -c '%s' $TMP_FILE` && [ $size -gt 0 ] \
227         || ret=1
228     if [ $ret -eq 0 ]; then
229         if [ -e $IPTABLES_DATA ]; then
230             cp -f $IPTABLES_DATA $IPTABLES_DATA.save \
231                 && chmod 600 $IPTABLES_DATA.save \
232                 || ret=1
233         fi
234         if [ $ret -eq 0 ]; then
235             cp -f $TMP_FILE $IPTABLES_DATA \
236                 && chmod 600 $IPTABLES_DATA \
237                 || ret=1
238         fi
239     fi
240     [ $ret -eq 0 ] && success || failure
241     echo
242     rm -f $TMP_FILE
243     return $ret
244 }
245
246 status() {
247     # Do not print status if lockfile is missing and iptables modules are not 
248     # loaded.
249     # Check if iptable module is loaded
250     if [ ! -f "$VAR_SUBSYS_IPTABLES" ]; then
251         echo $"Firewall is stopped."
252         return 1
253     fi
254
255     # Check if firewall is configured (has tables)
256     if [ ! -e "$PROC_IPTABLES_NAMES" ]; then
257         echo $"Firewall is not configured. "
258         return 1
259     fi
260     tables=`cat $PROC_IPTABLES_NAMES 2>/dev/null`
261     if [ -z "$tables" ]; then
262         echo $"Firewall is not configured. "
263         return 1
264     fi
265
266     NUM=
267     [ "x$IPTABLES_STATUS_NUMERIC" = "xyes" ] && NUM="-n"
268
269     for table in $tables; do
270         echo $"Table: $table"
271         $IPTABLES -t $table --list $NUM && echo
272     done
273
274     return 0
275 }
276
277 restart() {
278     [ "x$IPTABLES_SAVE_ON_RESTART" = "xyes" ] && save
279     stop
280     start
281 }
282
283 case "$1" in
284     start)
285         stop
286         start
287         RETVAL=$?
288         ;;
289     stop)
290         [ "x$IPTABLES_SAVE_ON_STOP" = "xyes" ] && save
291         stop
292         RETVAL=$?
293         ;;
294     restart)
295         restart
296         RETVAL=$?
297         ;;
298     condrestart)
299         [ -e "$VAR_SUBSYS_IPTABLES" ] && restart
300         ;;
301     status)
302         status
303         RETVAL=$?
304         ;;
305     panic)
306         flush_n_delete
307         set_policy DROP
308         RETVAL=$?
309         ;;
310     save)
311         save
312         RETVAL=$?
313         ;;
314     *)
315         echo $"Usage: $0 {start|stop|restart|condrestart|status|panic|save}"
316         exit 1
317         ;;
318 esac
319
320 exit $RETVAL