- Update paketa za Lenny
[iptables-cn.git] / debian / init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          iptables
5 # Required-Start:    $local_fs $remote_fs
6 # Required-Stop:     $local_fs $remote_fs
7 # Should-Start:      $syslog
8 # Should-Stop:       $syslog
9 # Default-Start:     2 3 4 5
10 # Default-Stop:      0 1 6
11 # Short-Description: Start or stop the iptables.
12 ### END INIT INFO
13
14 set -e
15
16 # Q: How do I get started?
17 # A: (Did I mention "do not use it" already? Oh well.)
18 #    1. Setup your normal iptables rules -- firewalling, port forwarding
19 #       NAT, etc. When everything is configured the way you like, run:
20 #
21 #           /etc/init.d/iptables save active
22 #
23 #    2. Setup your your inactive firewall rules -- this can be something
24 #       like clear all rules and set all policy defaults to accept (which
25 #       can be done with /etc/init.d/iptables clear). When that is ready,
26 #       save the inactive ruleset:
27 #
28 #           /etc/init.d/iptables save inactive
29 #
30 #    3. Controlling the script itself is done through runlevels configured
31 #       with debconf for package installation. Run "dpkg-reconfigure iptables"
32 #       to enable or disable after installation.
33 #
34 # Q: Is that all?
35 # A: Mostly. You can save additional rulesets and restore them by name. As
36 #    an example:
37 #
38 #       /etc/init.d/iptables save midnight
39 #       /etc/init.d/iptables load midnight
40 #
41 #
42 #    Autosave only works with start followed by stop.
43 #
44 #    Also, take great care with the halt option. It's almost as good as
45 #    pulling the network cable, except it disrupts localhost too.
46 #
47 #    Also, create the /var/lib/iptables and /var/lib/ip6tables dirs
48 #    as necessary.
49
50 # enable ipv6 support
51 enable_ipv6=false
52
53 # set enable_autosave to "true" to autosave the active ruleset
54 # when going from start to stop
55 enable_autosave=false
56
57 # set enable_save_counters to "true" to save table counters with
58 # rulesets
59 enable_save_counters=true
60
61 if test -f /etc/default/iptables-cn; then
62     . /etc/default/iptables-cn
63 fi
64
65 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
66
67 initd="$0"
68 default="$0"
69
70 initd_abort () {
71   cmd=$1
72   shift
73   echo "Aborting iptables $cmd: $@."
74   echo
75   usage
76   exit 0
77 }
78
79 initd_have_a_cow_man () {
80   for i in $@; do
81     if ! command -v "$i" >/dev/null 2>&1; then
82       echo "Aborting iptables initd: no $i executable"
83       exit 0
84     fi
85   done
86 }
87
88 initd_clear () {
89   rm -f "$autosave"
90   echo -n "Clearing ${iptables_command} ruleset: default ACCEPT policy"
91   $iptables_save | sed "/-/d;/^#/d;s/DROP/ACCEPT/" | $iptables_restore
92   echo "."
93 }
94
95 initd_halt () {
96   rm -f $autosave
97   echo -n "Clearing ${iptables_command} ruleset: default DROP policy"
98   $iptables_save | sed "/-/d;/^#/d;s/ACCEPT/DROP/" | $iptables_restore
99   echo "."
100 }
101
102 initd_load () {
103   ruleset="$libdir/$@"
104   if ! test -f "$ruleset"; then
105     initd_abort load "unknown ruleset, \"$@\""
106   fi
107   if test "$@" = inactive; then
108     initd_autosave
109   fi
110   rm -f "$autosave"
111   echo -n "Loading ${iptables_command} ruleset: load \"$@\""
112   $iptables_restore < "$ruleset"
113   echo "."
114 }
115
116 initd_counters () {
117   if test "${enable_save_counters:-false}" = true; then
118     echo -n " with counters"
119     $iptables_save -c > "$ruleset"
120   else
121     $iptables_save | sed '/^:/s@\[[0-9]\{1,\}:[0-9]\{1,\}\]@[0:0]@g' > "$ruleset"
122   fi
123 }
124
125 initd_save () {
126   rm -f $autosave
127   ruleset="${libdir}/$@"
128   echo -n "Saving ${iptables_command} ruleset: save \"$@\""
129    initd_counters
130   echo "."
131 }
132
133 initd_autosave () {
134   if test -f $autosave -a ${enable_autosave-false} = true; then
135     ruleset="${libdir}/active"
136     echo -n "Autosaving ${iptables_command} ruleset: save \"active\""
137     initd_counters
138     echo "."
139   fi
140 }
141
142 usage () {
143 #  current="$(ls -m ${libdir} \
144 #    | sed 's/ \{0,1\}autosave,\{0,1\} \{0,1\}//')"
145 cat << END
146 $initd options:
147   start|restart|reload|force-reload
148      load the "active" ruleset
149   save <ruleset>
150      save the current ruleset
151   load <ruleset>
152      load a ruleset
153   stop
154      load the "inactive" ruleset
155   clear
156      remove all rules and user-defined chains, set default policy to ACCEPT
157   halt
158      remove all rules and user-defined chains, set default policy to DROP
159
160 Saved ruleset locations: /var/lib/iptables/ and /var/lib/ip6tables/
161
162 Please read: $default
163
164 END
165 }
166
167 initd_main () {
168   case "$1" in
169     start|restart|reload|force-reload)
170       initd_load active
171       if test ${enable_autosave-false} = true; then
172         touch $autosave
173       fi
174       ;;
175     stop)
176       initd_load inactive
177       ;;
178     clear)
179       initd_clear
180       ;;
181     halt)
182       initd_halt
183       ;;
184     save)
185       shift
186       if test -z "$*"; then
187         initd_abort save "no ruleset name given"
188       else
189         initd_save "$*"
190       fi
191       ;;
192     load)
193       shift
194       if test -z "$*"; then
195         initd_abort load "no ruleset name given"
196       else
197         initd_load "$*"
198       fi
199       ;;
200     save_active) #legacy option
201       initd_save active
202       ;;
203     save_inactive) #legacy option
204       initd_save inactive
205       ;;
206     *)
207       echo "$initd: unknown command: \"$*\""
208       usage
209       ;;
210   esac
211 }
212
213 initd_preload() {
214   iptables="/sbin/${iptables_command}"
215   iptables_save="${iptables}-save"
216   iptables_restore="${iptables}-restore"
217   libdir="/var/lib/${iptables_command}"
218   autosave="${libdir}/autosave"
219   initd_have_a_cow_man "$iptables_save" "$iptables_restore"
220   ${iptables_command} -nL >/dev/null
221   initd_main $*
222 }
223
224 iptables_command=iptables initd_preload $*
225 if test "$enable_ipv6" = "true"; then
226   iptables_command=ip6tables initd_preload $*
227 fi
228
229 exit 0