aboutsummaryrefslogtreecommitdiff
path: root/examples/var_service/fw
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-06 04:04:19 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-06 04:04:19 +0100
commit6cf7f01256c39677a0a5561ebca60e8def9d6d7e (patch)
tree9751616a6653806d6703da369616d74e38f8b785 /examples/var_service/fw
parent85bb843f47342b19c4f0814331c1f4c78b0011ad (diff)
downloadbusybox-w32-6cf7f01256c39677a0a5561ebca60e8def9d6d7e.tar.gz
busybox-w32-6cf7f01256c39677a0a5561ebca60e8def9d6d7e.tar.bz2
busybox-w32-6cf7f01256c39677a0a5561ebca60e8def9d6d7e.zip
adding example runit-style service directory
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'examples/var_service/fw')
-rw-r--r--examples/var_service/fw/conf/11.22.33.44.ipconf--10
-rw-r--r--examples/var_service/fw/conf/192.168.0.1.ipconf11
-rw-r--r--examples/var_service/fw/conf/lo.ipconf10
-rw-r--r--examples/var_service/fw/etc/hosts21
-rw-r--r--examples/var_service/fw/etc/resolv.conf31
-rwxr-xr-xexamples/var_service/fw/run211
-rwxr-xr-xexamples/var_service/fw/stat12
7 files changed, 306 insertions, 0 deletions
diff --git a/examples/var_service/fw/conf/11.22.33.44.ipconf-- b/examples/var_service/fw/conf/11.22.33.44.ipconf--
new file mode 100644
index 000000000..9b44e9048
--- /dev/null
+++ b/examples/var_service/fw/conf/11.22.33.44.ipconf--
@@ -0,0 +1,10 @@
1#!/bin/sh
2# If we have simple static address...
3#
4let cfg=cfg+1
5if[$cfg]=if
6ip[$cfg]=11.22.33.44
7ipmask[$cfg]=11.22.33.44/24
8gw[$cfg]=11.22.33.1
9net[$cfg]=0/0
10dns[$cfg]='11.22.33.2 11.22.33.3'
diff --git a/examples/var_service/fw/conf/192.168.0.1.ipconf b/examples/var_service/fw/conf/192.168.0.1.ipconf
new file mode 100644
index 000000000..5cf55dbc7
--- /dev/null
+++ b/examples/var_service/fw/conf/192.168.0.1.ipconf
@@ -0,0 +1,11 @@
1#!/bin/sh
2# A small network with no routers
3# (maybe *we* are their router)
4#
5let cfg=cfg+1
6if[$cfg]=if
7ip[$cfg]=192.168.0.1
8ipmask[$cfg]=192.168.0.1/24
9### gw[$cfg]=
10### net[$cfg]=0/0
11### dns[$cfg]=''
diff --git a/examples/var_service/fw/conf/lo.ipconf b/examples/var_service/fw/conf/lo.ipconf
new file mode 100644
index 000000000..e6be5f063
--- /dev/null
+++ b/examples/var_service/fw/conf/lo.ipconf
@@ -0,0 +1,10 @@
1#!/bin/bash
2# Mostly redundant except when you need dns[]=your_static_dns_srv
3#
4let cfg=cfg+1
5if[$cfg]=lo
6ip[$cfg]=127.0.0.1
7ipmask[$cfg]=127.0.0.1/8
8gw[$cfg]=''
9net[$cfg]=''
10#dns[$cfg]=127.0.0.1
diff --git a/examples/var_service/fw/etc/hosts b/examples/var_service/fw/etc/hosts
new file mode 100644
index 000000000..f7ee533d2
--- /dev/null
+++ b/examples/var_service/fw/etc/hosts
@@ -0,0 +1,21 @@
1#!/bin/sh
2echo "\
3# This file is automagically regenerated
4# Note! /etc/nsswitch.conf may override this!
5
6# For loopbacking
7127.0.0.1 localhost
8
9# Our local IPs"
10
11hostname=`hostname`
12test "$hostname" || hostname=localhost
13domain=`(. /boot.conf; echo "$DNSDOMAINNAME")`
14test "$domain" && hostname="$hostname $hostname.$domain"
15
16ip -o a l \
17| grep -F 'inet ' \
18| sed -e 's/^.*inet //' -e 's:[ /].*$: '"$hostname"':'
19
20echo
21echo "# End of /etc/hosts"
diff --git a/examples/var_service/fw/etc/resolv.conf b/examples/var_service/fw/etc/resolv.conf
new file mode 100644
index 000000000..3f37b86f5
--- /dev/null
+++ b/examples/var_service/fw/etc/resolv.conf
@@ -0,0 +1,31 @@
1#!/bin/bash
2
3domain=`(. /boot.conf; echo "$DNSDOMAINNAME") 2>/dev/null`
4
5echo "# This file is automagically regenerated with each boot"
6echo
7test "$domain" && echo "domain $domain"
8test "$domain" && echo "search $domain"
9echo
10echo "# Note that nslookup can choke on DNS server which itself"
11echo "# does NOT have domain name. Other things can work fine."
12echo
13# # If we run DNS cache:
14# echo "nameserver 127.0.0.1"
15# exit
16
17prio=0
18i=0; while test "${if[$i]}"; do
19 test x"${dns_prio[$i]}" != x"" \
20 && test "${dns_prio[$i]}" -gt "$prio" \
21 && prio="${dns_prio[$i]}"
22let i++; done
23
24i=0; while test "${if[$i]}"; do
25 for d in ${dns[$i]}; do
26 p="${dns_prio[$i]}"
27 test x"$p" == x"" && p=0
28 test x"$p" == x"$prio" || continue
29 echo "nameserver $d"
30 done
31let i++; done
diff --git a/examples/var_service/fw/run b/examples/var_service/fw/run
new file mode 100755
index 000000000..f02f53dc1
--- /dev/null
+++ b/examples/var_service/fw/run
@@ -0,0 +1,211 @@
1#!/bin/bash
2# (using bashisms: "function", arrays)
3
4user=root
5extif=if
6ext_open_tcp="21 22 80" # space-separated
7
8# Make ourself one-shot
9sv o .
10# Debug
11#date '+%Y-%m-%d %H:%M:%S' >>"$0.log"
12
13service=`basename "$PWD"`
14rundir="/var/run/service/$service"
15
16### filter This is the default table (if no -t option is passed). It contains
17### the built-in chains INPUT (for packets coming into the box itself),
18### FORWARD (for packets being routed through the box), and OUTPUT (for
19### locally-generated packets).
20###
21### nat This table is consulted when a packet that creates a new connection
22### is encountered. It consists of three built-ins: PREROUTING (for
23### altering packets as soon as they come in), OUTPUT (for altering
24### locally-generated packets before routing), and POSTROUTING (for
25### altering packets as they are about to go out).
26###
27### mangle It had two built-in chains: PREROUTING (for altering incoming
28### packets before routing) and OUTPUT (for altering locally-generated
29### packets before routing). Recently three other built-in
30### chains are added: INPUT (for packets coming into the box
31### itself), FORWARD (for altering packets being routed through the
32### box), and POSTROUTING (for altering packets as they are about to go
33### out).
34###
35### ...iface... ...iface...
36### | ^
37### v |
38### -mangle,NAT- -mangle,filter- -mangle,NAT--
39### |PREROUTING|-->[Routing]-->|FORWARD |-->|POSTROUTING|
40### ------------ | ^ --------------- -------------
41### | | ^
42### | +--if NATed------------+ |
43### v | |
44### -mangle,filter- -mangle,NAT,filter-
45### |INPUT | +->[Routing]->|OUTPUT |
46### --------------- | -------------------
47### | |
48### v |
49### ... Local Process...
50
51doit() {
52 echo "# $*"
53 "$@"
54}
55
56#exec >/dev/null
57exec >"$0.out"
58exec 2>&1
59exec </dev/null
60
61umask 077
62
63# Make sure rundir/ exists
64mkdir -p "$rundir" 2>/dev/null
65chown -R "$user:" "$rundir"
66chmod -R a=rX "$rundir"
67rm -rf rundir 2>/dev/null
68ln -s "$rundir" rundir
69
70# Timestamping
71date '+%Y-%m-%d %H:%M:%S'
72
73
74echo; echo "* Reading IP config"
75cfg=-1
76# static cfg dhcp,zeroconf etc
77for ipconf in conf/*.ipconf "$rundir"/*.ipconf; do
78 if test -f "$ipconf"; then
79 echo "+ $ipconf"
80 . "$ipconf"
81 fi
82done
83
84echo; echo "* Configuring hardware"
85#doit ethtool -s if autoneg off speed 100 duplex full
86#doit ethtool -K if rx off tx off sg off tso off
87
88echo; echo "* Resetting address and routing info"
89doit ip a f dev lo
90i=0; while test "${if[$i]}"; do
91 doit ip a f dev "${if[$i]}"
92 doit ip r f dev "${if[$i]}" root 0/0
93let i++; done
94
95echo; echo "* Configuring addresses"
96doit ip a a dev lo 127.0.0.1/8 scope host
97doit ip a a dev lo ::1/128 scope host
98i=0; while test "${if[$i]}"; do
99 if test "${ipmask[$i]}"; then
100 doit ip a a dev "${if[$i]}" "${ipmask[$i]}" brd +
101 doit ip l set dev "${if[$i]}" up
102 fi
103let i++; done
104
105echo; echo "* Configuring routes"
106i=0; while test "${if[$i]}"; do
107 if test "${net[$i]}" && test "${gw[$i]}"; then
108 doit ip r a "${net[$i]}" via "${gw[$i]}"
109 fi
110let i++; done
111
112echo; echo "* Recreating /etc/* files reflecting new network configuration:"
113for i in etc/*; do
114 n=`basename "$i"`
115 echo "+ $n"
116 (. "$i") >"/etc/$n"
117 chmod 644 "/etc/$n"
118done
119
120
121# Usage: new_chain <chain> [<table>]
122new_chain() {
123 local t=""
124 test x"$2" != x"" && t="-t $2"
125 doit iptables $t -N $1
126 ipt="iptables $t -A $1"
127}
128
129echo; echo "* Reset iptables"
130doit iptables --flush
131doit iptables --delete-chain
132doit iptables --zero
133doit iptables -t nat --flush
134doit iptables -t nat --delete-chain
135doit iptables -t nat --zero
136doit iptables -t mangle --flush
137doit iptables -t mangle --delete-chain
138doit iptables -t mangle --zero
139
140echo; echo "* Configure iptables"
141doit modprobe nf_nat_ftp
142doit modprobe nf_nat_tftp
143doit modprobe nf_conntrack_ftp
144doit modprobe nf_conntrack_tftp
145
146# *** nat ***
147# INCOMING TRAFFIC
148ipt="iptables -t nat -A PREROUTING"
149# nothing here
150
151# LOCALLY ORIGINATED TRAFFIC
152ipt="iptables -t nat -A OUTPUT"
153# nothing here
154
155# OUTGOING TRAFFIC
156ipt="iptables -t nat -A POSTROUTING"
157# Masquerade boxes on my private net
158doit $ipt -s 192.168.0.0/24 -o $extif -j MASQUERADE
159
160# *** mangle ***
161### DEBUG
162### ipt="iptables -t mangle -A PREROUTING"
163### doit $ipt -s 192.168.0.0/24 -j RETURN
164### ipt="iptables -t mangle -A FORWARD"
165### doit $ipt -s 192.168.0.0/24 -j RETURN
166### ipt="iptables -t mangle -A POSTROUTING"
167### doit $ipt -s 192.168.0.0/24 -j RETURN
168# nothing here
169
170# *** filter ***
171#
172new_chain iext filter
173#doit $ipt -s 203.177.104.72 -j DROP # Some idiot probes my ssh
174#doit $ipt -d 203.177.104.72 -j DROP # Some idiot probes my ssh
175doit $ipt -m state --state ESTABLISHED,RELATED -j RETURN # FTP data etc is ok
176if test "$ext_open_tcp"; then
177 portlist="${ext_open_tcp// /,}"
178 doit $ipt -p tcp -m multiport --dports $portlist -j RETURN
179fi
180doit $ipt -p tcp -j REJECT # Anything else isn't ok. REJECT = irc opens faster
181 # (it probes proxy ports, DROP will incur timeout delays)
182ipt="iptables -t filter -A INPUT"
183doit $ipt -i $extif -j iext
184
185
186echo; echo "* Enabling forwarding"
187echo 1 >/proc/sys/net/ipv4/ip_forward
188echo "/proc/sys/net/ipv4/ip_forward: `cat /proc/sys/net/ipv4/ip_forward`"
189
190
191# Signal everybody that firewall is up
192date '+%Y-%m-%d %H:%M:%S' >"$rundir/up"
193
194# Ok, spew out gobs of info and disable ourself
195echo; echo "* IP:"
196ip a l
197echo; echo "* Routing:"
198ip r l
199echo; echo "* Firewall:"
200{
201echo '---FILTER--';
202iptables -v -L -x -n;
203echo '---NAT-----';
204iptables -t nat -v -L -x -n;
205echo '---MANGLE--';
206iptables -t mangle -v -L -x -n;
207} \
208| grep -v '^$' | grep -Fv 'bytes target'
209echo
210
211echo "* End of firewall configuration"
diff --git a/examples/var_service/fw/stat b/examples/var_service/fw/stat
new file mode 100755
index 000000000..08736ada8
--- /dev/null
+++ b/examples/var_service/fw/stat
@@ -0,0 +1,12 @@
1#!/bin/sh
2
3echo; echo "* Firewall:"
4{
5echo '---FILTER--';
6iptables -v -L -x -n;
7echo '---NAT-----';
8iptables -t nat -v -L -x -n;
9echo '---MANGLE--';
10iptables -t mangle -v -L -x -n;
11} \
12| grep -v '^$' | grep -Fv 'bytes target' | $PAGER