]> git.pld-linux.org Git - packages/iptables.git/blob - iptables.init
- added link patch: don't propagate -lpcap everywhere; release 2
[packages/iptables.git] / iptables.init
1 #!/bin/sh
2 #
3 # Startup script to implement /etc/sysconfig/iptables pre-defined rules.
4 #
5 # chkconfig: 2345 08 92
6 #
7 # description: Automates a packet filtering firewall with iptables.
8 #
9 # by bero@redhat.com, based on the ipchains script:
10 # Script Author:        Joshua Jensen <joshua@redhat.com>
11 #   -- hacked up by gafton with help from notting
12 # modified by Anton Altaparmakov <aia21@cam.ac.uk>:
13 # modified by Nils Philippsen <nils@redhat.de>
14 #
15 # config: /etc/sysconfig/iptables
16
17 IPTABLES_CONFIG=/etc/sysconfig/iptables
18 if [ ! -f $IPTABLES_CONFIG ]; then
19         case "$1" in
20         start|restart|force-reload)
21                 exit 0
22         ;;
23         esac
24 fi
25
26 # Source 'em up
27 . /etc/rc.d/init.d/functions
28
29 if [ "$(kernelver)" -lt "002003000" ]; then
30         exit 0
31 fi
32
33 if /sbin/lsmod 2>/dev/null | grep -q ipchains; then
34         # Don't do both
35         exit 0
36 fi
37
38 IPTABLES_SAVE_ON_STOP="no"
39 IPTABLES_SAVE_COUNTER="no"
40 IPTABLES_STATUS_NUMERIC="yes"
41 IPTABLES_STATUS_VERBOSE="no"
42 IPTABLES_STATUS_LINENUMBERS="yes"
43 [ -f /etc/sysconfig/iptables-config ] && . /etc/sysconfig/iptables-config
44 _SAVEOPT=
45 is_yes $IPTABLES_SAVE_COUNTER && _SAVEOPT="-c"
46
47 if [ "$1" = "--quiet" ]; then
48         shift
49         show() { return 0; }
50         ok() { return 0; }
51         fail() { return 1; }
52 fi
53
54 iftable() {
55         if fgrep -qsx $1 /proc/net/ip_tables_names; then
56                 iptables -t "$@"
57         fi
58 }
59
60 start() {
61         # don't do squat if we don't have the config file
62         if [ -f $IPTABLES_CONFIG ]; then
63                 # If we don't clear these first, we might be adding to
64                 #  pre-existing rules.
65                 tables=`cat /proc/net/ip_tables_names 2>/dev/null`
66                 show "Flushing all current rules and user defined chains"
67                 let ret=0
68                 for i in $tables; do iptables -t $i -F; let ret+=$?; done
69                 if [ $ret -eq 0 ]; then
70                         ok
71                 else
72                         fail
73                 fi
74                 show "Clearing all current rules and user defined chains"
75                 let ret=0
76                 for i in $tables; do iptables -t $i -X; let ret+=$?; done
77                 if [ $ret -eq 0 ]; then
78                         ok
79                 else
80                         fail
81                 fi
82
83                 for i in $tables; do iptables -t $i -Z; done
84
85                 show "Applying iptables firewall rules"
86                 grep -v "^[[:space:]]*#" $IPTABLES_CONFIG | grep -v '^[[:space:]]*$' | /usr/sbin/iptables-restore $_SAVEOPT && \
87                         ok || \
88                         fail
89                 touch /var/lock/subsys/iptables
90         fi
91 }
92
93 stop() {
94         tables=`cat /proc/net/ip_tables_names 2>/dev/null`
95         show "Flushing all chains"
96         let ret=0
97         for i in $tables; do iptables -t $i -F; let ret+=$?; done
98         if [ $ret -eq 0 ]; then
99                 ok
100         else
101                 fail
102         fi
103
104         show "Removing user defined chains"
105         let ret=0
106         for i in $tables; do iptables -t $i -X; let ret+=$?; done
107         if [ $ret -eq 0 ]; then
108                 ok
109         else
110                 fail
111         fi
112         show "Resetting built-in chains to the default ACCEPT policy"
113         iftable filter -P INPUT ACCEPT && \
114         iftable filter -P OUTPUT ACCEPT && \
115         iftable filter -P FORWARD ACCEPT && \
116         iftable nat -P PREROUTING ACCEPT && \
117         iftable nat -P POSTROUTING ACCEPT && \
118         iftable nat -P OUTPUT ACCEPT && \
119         iftable mangle -P PREROUTING ACCEPT && \
120         iftable mangle -P OUTPUT ACCEPT && \
121         ok || fail
122         rm -f /var/lock/subsys/iptables
123 }
124
125 save() {
126         show "Saving current rules to %s" $IPTABLES_CONFIG
127         touch $IPTABLES_CONFIG
128         chmod 600 $IPTABLES_CONFIG
129         /usr/sbin/iptables-save $_SAVEOPT > $IPTABLES_CONFIG  2>/dev/null && ok || fail
130 }
131
132 upstart_controlled --except status panic load save clear
133
134 case "$1" in
135   start|load)
136         start
137         ;;
138   stop)
139         is_yes $IPTABLES_SAVE_ON_STOP && save
140         stop
141         ;;
142   clear)
143         stop
144         ;;
145   restart|force-reload)
146         # "restart" is really just "start" as this isn't a daemon,
147         #  and "start" clears any pre-defined rules anyway.
148         #  This is really only here to make those who expect it happy
149         start
150         ;;
151   panic)
152         show "Changing target policies to DROP"
153         iftable filter -P INPUT DROP && \
154         iftable filter -P FORWARD DROP && \
155         iftable filter -P OUTPUT DROP && \
156         iftable nat -P PREROUTING DROP && \
157         iftable nat -P POSTROUTING DROP && \
158         iftable nat -P OUTPUT DROP && \
159         iftable mangle -P PREROUTING DROP && \
160         iftable mangle -P OUTPUT DROP && \
161         ok || fail
162         iftable filter -F INPUT && \
163         iftable filter -F FORWARD && \
164         iftable filter -F OUTPUT && \
165         iftable nat -F PREROUTING && \
166         iftable nat -F POSTROUTING && \
167         iftable nat -F OUTPUT && \
168         iftable mangle -F PREROUTING && \
169         iftable mangle -F OUTPUT && \
170         ok || fail
171         iftable filter -X INPUT && \
172         iftable filter -X FORWARD && \
173         iftable filter -X OUTPUT && \
174         iftable nat -X PREROUTING && \
175         iftable nat -X POSTROUTING && \
176         iftable nat -X OUTPUT && \
177         iftable mangle -X PREROUTING && \
178         iftable mangle -X OUTPUT && \
179         ok || fail
180         ;;
181   save)
182         save
183         ;;
184   status)
185         is_yes $IPTABLES_STATUS_NUMERIC && _NUMERIC="-n"
186         is_yes $IPTABLES_STATUS_VERBOSE && _VERBOSE="--verbose"
187         is_yes $IPTABLES_STATUS_LINENUMBERS && _LINES="--line-numbers"
188         tables=`cat /proc/net/ip_tables_names 2>/dev/null`
189         for table in $tables; do
190                 echo "Table: $table"
191                 iptables -t $table --list $_NUMERIC $_VERBOSE $_LINES
192         done
193         ;;
194   *)
195         msg_usage "$0 {start|stop|restart|force-reload|panic|load|save|clear|status}"
196         exit 3
197 esac
198
199 exit 0
This page took 0.080976 seconds and 3 git commands to generate.