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