diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-06 04:04:19 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-06 04:04:19 +0100 |
| commit | 6cf7f01256c39677a0a5561ebca60e8def9d6d7e (patch) | |
| tree | 9751616a6653806d6703da369616d74e38f8b785 /examples | |
| parent | 85bb843f47342b19c4f0814331c1f4c78b0011ad (diff) | |
| download | busybox-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')
20 files changed, 613 insertions, 0 deletions
diff --git a/examples/var_service/dhcp_if/convert2ipconf b/examples/var_service/dhcp_if/convert2ipconf new file mode 100755 index 000000000..cee085463 --- /dev/null +++ b/examples/var_service/dhcp_if/convert2ipconf | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | # convert: | ||
| 3 | |||
| 4 | # dhcptype=5 | ||
| 5 | # serverid=172.16.42.102 | ||
| 6 | # lease=97200 | ||
| 7 | # interface=eth0 | ||
| 8 | # ip=172.16.42.177 | ||
| 9 | # subnet=255.255.255.0 | ||
| 10 | # mask=24 | ||
| 11 | # broadcast=172.16.22.255 | ||
| 12 | # router=172.16.42.98 | ||
| 13 | # dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 | ||
| 14 | # domain=lab.example.com example.com | ||
| 15 | # ntpsrv=10.34.32.125 10.34.255.7 | ||
| 16 | |||
| 17 | # into: | ||
| 18 | |||
| 19 | #let cfg=cfg+1 | ||
| 20 | #if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=... | ||
| 21 | |||
| 22 | exec >"$0.out" 2>&1 | ||
| 23 | |||
| 24 | test "$interface" || exit 1 | ||
| 25 | test -f "$1" || exit 1 | ||
| 26 | |||
| 27 | # Unsafe, and does not handle values with spaces: | ||
| 28 | #. "./$1" || exit 1 | ||
| 29 | # Safe(r) parsing: | ||
| 30 | sq="'" | ||
| 31 | while read line; do | ||
| 32 | #echo "line: $line" | ||
| 33 | # Skip empty lines and lines with single quotes | ||
| 34 | test "${line##*$sq*}" || continue | ||
| 35 | var="${line%%=*}" | ||
| 36 | val="${line#*=}" | ||
| 37 | #echo "var:$var val:'$val'" | ||
| 38 | eval "$var='$val'" | ||
| 39 | done <"$1" | ||
| 40 | |||
| 41 | { | ||
| 42 | echo "let cfg=cfg+1" | ||
| 43 | test "$interface" && echo "if[\$cfg]='$interface'" | ||
| 44 | test "$ip" && echo "ip[\$cfg]='$ip'" | ||
| 45 | test "$ip" && test "$mask" \ | ||
| 46 | && echo "ipmask[\$cfg]='$ip/$mask'" | ||
| 47 | test "$router" && echo "gw[\$cfg]='$router'" | ||
| 48 | test "$dns" && echo "dns[\$cfg]='$dns'" | ||
| 49 | # TODO: I never saw a dhcp server which correctly announces | ||
| 50 | # which subnet(s) is/are available thru advertised router | ||
| 51 | # Assume 0/0 | ||
| 52 | echo "net[\$cfg]='0/0'" | ||
| 53 | } >"$2" | ||
diff --git a/examples/var_service/dhcp_if/dhcp_handler b/examples/var_service/dhcp_if/dhcp_handler new file mode 100755 index 000000000..9ed3e7a3f --- /dev/null +++ b/examples/var_service/dhcp_if/dhcp_handler | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | # executed by udhcpc | ||
| 3 | # parameters: $1 and environment | ||
| 4 | # | ||
| 5 | # $1 is: | ||
| 6 | # | ||
| 7 | # deconfig: This argument is used when udhcpc starts, and | ||
| 8 | # when a lease is lost. The script should put the interface in an | ||
| 9 | # up, but deconfigured state, ie: ifconfig $interface 0.0.0.0. | ||
| 10 | # Environment: interface=ethN | ||
| 11 | # | ||
| 12 | # bound: This argument is used when udhcpc moves from an | ||
| 13 | # unbound, to a bound state. All of the paramaters are set in | ||
| 14 | # enviromental variables, The script should configure the interface, | ||
| 15 | # and set any other relavent parameters (default gateway, dns server, etc). | ||
| 16 | # Environment: | ||
| 17 | # dhcptype=5 | ||
| 18 | # serverid=172.16.42.102 | ||
| 19 | # lease=97200 | ||
| 20 | # interface=eth0 | ||
| 21 | # ip=172.16.42.177 | ||
| 22 | # subnet=255.255.255.0 | ||
| 23 | # mask=24 | ||
| 24 | # broadcast=172.16.22.255 | ||
| 25 | # router=172.16.42.98 | ||
| 26 | # dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 | ||
| 27 | # domain=lab.example.com example.com | ||
| 28 | # ntpsrv=10.34.32.125 10.34.255.7 | ||
| 29 | # | ||
| 30 | # renew: This argument is used when a DHCP lease is renewed. All of | ||
| 31 | # the paramaters are set in enviromental variables. This argument is | ||
| 32 | # used when the interface is already configured, so the IP address, | ||
| 33 | # will not change, however, the other DHCP paramaters, such as the | ||
| 34 | # default gateway, subnet mask, and dns server may change. | ||
| 35 | # Environment: same as for "bound". | ||
| 36 | # | ||
| 37 | # nak: This argument is used with udhcpc receives a NAK message. | ||
| 38 | # The script with the deconfig argument will be called directly | ||
| 39 | # afterwards, so no changes to the network interface are neccessary. | ||
| 40 | # This hook is provided for purely informational purposes (the | ||
| 41 | # message option may contain a reason for the NAK). | ||
| 42 | # Environment: interface=ethN, serverid=IP_ADDR | ||
| 43 | # | ||
| 44 | # leasefail: called when lease cannot be obtained | ||
| 45 | # (for example, when DHCP server is down). | ||
| 46 | # Environment: interface=ethN | ||
| 47 | |||
| 48 | # TODO: put domain into /etc/resolv.conf (thru /var/service/fw) | ||
| 49 | # TODO: feed ntp IPs to /var/service/ntp | ||
| 50 | |||
| 51 | service=`basename $PWD` | ||
| 52 | outfile="$service.ipconf" | ||
| 53 | dir="/var/run/service/fw" | ||
| 54 | |||
| 55 | exec >>"$0.out" 2>&1 | ||
| 56 | |||
| 57 | echo "`date`: Params: $*" | ||
| 58 | |||
| 59 | if test x"$1" != x"bound" && test x"$1" != x"renew" ; then | ||
| 60 | # Reconfigure network with this interface disabled | ||
| 61 | echo "Deconfiguring" | ||
| 62 | rm "$service.out" | ||
| 63 | rm "$outfile" | ||
| 64 | rm "$dir/$outfile" | ||
| 65 | sv u /var/service/fw | ||
| 66 | exit | ||
| 67 | fi | ||
| 68 | |||
| 69 | # Bound: we've got the lease | ||
| 70 | |||
| 71 | # Process params | ||
| 72 | env >"$service.out" | ||
| 73 | ./convert2ipconf "$service.out" "$outfile" | ||
| 74 | |||
| 75 | # Reconfigure routing and firewall if needed | ||
| 76 | diff --brief "$outfile" "$dir/$outfile" >/dev/null 2>&1 | ||
| 77 | if test "$?" != "0"; then | ||
| 78 | echo "Reconfiguring" | ||
| 79 | mkdir -p "$dir" 2>/dev/null | ||
| 80 | cp "$outfile" "$dir/$outfile" | ||
| 81 | sv u /var/service/fw | ||
| 82 | fi | ||
diff --git a/examples/var_service/dhcp_if/log/run b/examples/var_service/dhcp_if/log/run new file mode 100755 index 000000000..560d1b19f --- /dev/null +++ b/examples/var_service/dhcp_if/log/run | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | user=logger | ||
| 4 | |||
| 5 | logdir="/var/log/service/`(cd ..;basename $PWD)`" | ||
| 6 | mkdir -p "$logdir" 2>/dev/null | ||
| 7 | chown -R "$user": "$logdir" | ||
| 8 | chmod -R go-rwxst,u+rwX "$logdir" | ||
| 9 | rm logdir | ||
| 10 | ln -s "$logdir" logdir | ||
| 11 | |||
| 12 | # make this dir accessible to logger | ||
| 13 | chmod a+rX . | ||
| 14 | |||
| 15 | exec >/dev/null | ||
| 16 | exec 2>&1 | ||
| 17 | exec \ | ||
| 18 | env - PATH="$PATH" \ | ||
| 19 | softlimit \ | ||
| 20 | setuidgid "$user" \ | ||
| 21 | svlogd -tt "$logdir" | ||
diff --git a/examples/var_service/dhcp_if/p_log b/examples/var_service/dhcp_if/p_log new file mode 100755 index 000000000..a2521be05 --- /dev/null +++ b/examples/var_service/dhcp_if/p_log | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | cd log/logdir || exit 1 | ||
| 4 | cat @* current | $PAGER | ||
diff --git a/examples/var_service/dhcp_if/run b/examples/var_service/dhcp_if/run new file mode 100755 index 000000000..aec79e027 --- /dev/null +++ b/examples/var_service/dhcp_if/run | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | exec 2>&1 | ||
| 4 | exec </dev/null | ||
| 5 | |||
| 6 | pwd="$PWD" | ||
| 7 | |||
| 8 | if="${PWD##*/dhcp_}" | ||
| 9 | |||
| 10 | echo "* Upping iface $if" | ||
| 11 | ip link set dev "$if" up | ||
| 12 | |||
| 13 | echo "* Starting udhcpc" | ||
| 14 | exec \ | ||
| 15 | env - PATH="$PATH" \ | ||
| 16 | softlimit \ | ||
| 17 | setuidgid root \ | ||
| 18 | udhcpc -vv \ | ||
| 19 | --hostname=null \ | ||
| 20 | --foreground \ | ||
| 21 | --interface="$if" \ | ||
| 22 | --pidfile="$pwd/udhcpc.pid" \ | ||
| 23 | --script="$pwd/dhcp_handler" | ||
diff --git a/examples/var_service/dhcp_if/w_log b/examples/var_service/dhcp_if/w_log new file mode 100755 index 000000000..34b19b373 --- /dev/null +++ b/examples/var_service/dhcp_if/w_log | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | cd log/logdir || exit 1 | ||
| 4 | watch -n2 'w=`ttysize w`; h=`ttysize h`; tail -$((h-3)) current 2>&1 | cut -b0-$((w-2))' | ||
diff --git a/examples/var_service/dhcp_if_pinger/run b/examples/var_service/dhcp_if_pinger/run new file mode 100755 index 000000000..20b2fc516 --- /dev/null +++ b/examples/var_service/dhcp_if_pinger/run | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | delay=67 | ||
| 4 | |||
| 5 | if=${PWD##*/dhcp_} | ||
| 6 | if=${if%%_pinger} | ||
| 7 | |||
| 8 | if test -f "$0.log"; then | ||
| 9 | tail -999 "$0.log" >"$0.log.new" | ||
| 10 | mv "$0.log.new" "$0.log" | ||
| 11 | fi | ||
| 12 | |||
| 13 | test -f "/var/service/dhcp_$if/dhcp_$if.out" || exec env - sleep "$delay" | ||
| 14 | . "/var/service/dhcp_$if/dhcp_$if.out" | ||
| 15 | test x"$router" != x"" || exec env - sleep "$delay" | ||
| 16 | |||
| 17 | #echo "`date '+%Y-%m-%d %H:%M:%S'` Testing ping -c3 $router" >>"$0.log" | ||
| 18 | ping -c3 "$router" && exec env - sleep "$delay" | ||
| 19 | |||
| 20 | echo "`date '+%Y-%m-%d %H:%M:%S'` Restarting /var/service/dhcp_$if" >>"$0.log" | ||
| 21 | sv t "/var/service/dhcp_$if" | ||
| 22 | |||
| 23 | exec env - sleep "$delay" | ||
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 | # | ||
| 4 | let cfg=cfg+1 | ||
| 5 | if[$cfg]=if | ||
| 6 | ip[$cfg]=11.22.33.44 | ||
| 7 | ipmask[$cfg]=11.22.33.44/24 | ||
| 8 | gw[$cfg]=11.22.33.1 | ||
| 9 | net[$cfg]=0/0 | ||
| 10 | dns[$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 | # | ||
| 5 | let cfg=cfg+1 | ||
| 6 | if[$cfg]=if | ||
| 7 | ip[$cfg]=192.168.0.1 | ||
| 8 | ipmask[$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 | # | ||
| 4 | let cfg=cfg+1 | ||
| 5 | if[$cfg]=lo | ||
| 6 | ip[$cfg]=127.0.0.1 | ||
| 7 | ipmask[$cfg]=127.0.0.1/8 | ||
| 8 | gw[$cfg]='' | ||
| 9 | net[$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 | ||
| 2 | echo "\ | ||
| 3 | # This file is automagically regenerated | ||
| 4 | # Note! /etc/nsswitch.conf may override this! | ||
| 5 | |||
| 6 | # For loopbacking | ||
| 7 | 127.0.0.1 localhost | ||
| 8 | |||
| 9 | # Our local IPs" | ||
| 10 | |||
| 11 | hostname=`hostname` | ||
| 12 | test "$hostname" || hostname=localhost | ||
| 13 | domain=`(. /boot.conf; echo "$DNSDOMAINNAME")` | ||
| 14 | test "$domain" && hostname="$hostname $hostname.$domain" | ||
| 15 | |||
| 16 | ip -o a l \ | ||
| 17 | | grep -F 'inet ' \ | ||
| 18 | | sed -e 's/^.*inet //' -e 's:[ /].*$: '"$hostname"':' | ||
| 19 | |||
| 20 | echo | ||
| 21 | echo "# 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 | |||
| 3 | domain=`(. /boot.conf; echo "$DNSDOMAINNAME") 2>/dev/null` | ||
| 4 | |||
| 5 | echo "# This file is automagically regenerated with each boot" | ||
| 6 | echo | ||
| 7 | test "$domain" && echo "domain $domain" | ||
| 8 | test "$domain" && echo "search $domain" | ||
| 9 | echo | ||
| 10 | echo "# Note that nslookup can choke on DNS server which itself" | ||
| 11 | echo "# does NOT have domain name. Other things can work fine." | ||
| 12 | echo | ||
| 13 | # # If we run DNS cache: | ||
| 14 | # echo "nameserver 127.0.0.1" | ||
| 15 | # exit | ||
| 16 | |||
| 17 | prio=0 | ||
| 18 | i=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]}" | ||
| 22 | let i++; done | ||
| 23 | |||
| 24 | i=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 | ||
| 31 | let 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 | |||
| 4 | user=root | ||
| 5 | extif=if | ||
| 6 | ext_open_tcp="21 22 80" # space-separated | ||
| 7 | |||
| 8 | # Make ourself one-shot | ||
| 9 | sv o . | ||
| 10 | # Debug | ||
| 11 | #date '+%Y-%m-%d %H:%M:%S' >>"$0.log" | ||
| 12 | |||
| 13 | service=`basename "$PWD"` | ||
| 14 | rundir="/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 | |||
| 51 | doit() { | ||
| 52 | echo "# $*" | ||
| 53 | "$@" | ||
| 54 | } | ||
| 55 | |||
| 56 | #exec >/dev/null | ||
| 57 | exec >"$0.out" | ||
| 58 | exec 2>&1 | ||
| 59 | exec </dev/null | ||
| 60 | |||
| 61 | umask 077 | ||
| 62 | |||
| 63 | # Make sure rundir/ exists | ||
| 64 | mkdir -p "$rundir" 2>/dev/null | ||
| 65 | chown -R "$user:" "$rundir" | ||
| 66 | chmod -R a=rX "$rundir" | ||
| 67 | rm -rf rundir 2>/dev/null | ||
| 68 | ln -s "$rundir" rundir | ||
| 69 | |||
| 70 | # Timestamping | ||
| 71 | date '+%Y-%m-%d %H:%M:%S' | ||
| 72 | |||
| 73 | |||
| 74 | echo; echo "* Reading IP config" | ||
| 75 | cfg=-1 | ||
| 76 | # static cfg dhcp,zeroconf etc | ||
| 77 | for ipconf in conf/*.ipconf "$rundir"/*.ipconf; do | ||
| 78 | if test -f "$ipconf"; then | ||
| 79 | echo "+ $ipconf" | ||
| 80 | . "$ipconf" | ||
| 81 | fi | ||
| 82 | done | ||
| 83 | |||
| 84 | echo; 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 | |||
| 88 | echo; echo "* Resetting address and routing info" | ||
| 89 | doit ip a f dev lo | ||
| 90 | i=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 | ||
| 93 | let i++; done | ||
| 94 | |||
| 95 | echo; echo "* Configuring addresses" | ||
| 96 | doit ip a a dev lo 127.0.0.1/8 scope host | ||
| 97 | doit ip a a dev lo ::1/128 scope host | ||
| 98 | i=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 | ||
| 103 | let i++; done | ||
| 104 | |||
| 105 | echo; echo "* Configuring routes" | ||
| 106 | i=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 | ||
| 110 | let i++; done | ||
| 111 | |||
| 112 | echo; echo "* Recreating /etc/* files reflecting new network configuration:" | ||
| 113 | for i in etc/*; do | ||
| 114 | n=`basename "$i"` | ||
| 115 | echo "+ $n" | ||
| 116 | (. "$i") >"/etc/$n" | ||
| 117 | chmod 644 "/etc/$n" | ||
| 118 | done | ||
| 119 | |||
| 120 | |||
| 121 | # Usage: new_chain <chain> [<table>] | ||
| 122 | new_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 | |||
| 129 | echo; echo "* Reset iptables" | ||
| 130 | doit iptables --flush | ||
| 131 | doit iptables --delete-chain | ||
| 132 | doit iptables --zero | ||
| 133 | doit iptables -t nat --flush | ||
| 134 | doit iptables -t nat --delete-chain | ||
| 135 | doit iptables -t nat --zero | ||
| 136 | doit iptables -t mangle --flush | ||
| 137 | doit iptables -t mangle --delete-chain | ||
| 138 | doit iptables -t mangle --zero | ||
| 139 | |||
| 140 | echo; echo "* Configure iptables" | ||
| 141 | doit modprobe nf_nat_ftp | ||
| 142 | doit modprobe nf_nat_tftp | ||
| 143 | doit modprobe nf_conntrack_ftp | ||
| 144 | doit modprobe nf_conntrack_tftp | ||
| 145 | |||
| 146 | # *** nat *** | ||
| 147 | # INCOMING TRAFFIC | ||
| 148 | ipt="iptables -t nat -A PREROUTING" | ||
| 149 | # nothing here | ||
| 150 | |||
| 151 | # LOCALLY ORIGINATED TRAFFIC | ||
| 152 | ipt="iptables -t nat -A OUTPUT" | ||
| 153 | # nothing here | ||
| 154 | |||
| 155 | # OUTGOING TRAFFIC | ||
| 156 | ipt="iptables -t nat -A POSTROUTING" | ||
| 157 | # Masquerade boxes on my private net | ||
| 158 | doit $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 | # | ||
| 172 | new_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 | ||
| 175 | doit $ipt -m state --state ESTABLISHED,RELATED -j RETURN # FTP data etc is ok | ||
| 176 | if test "$ext_open_tcp"; then | ||
| 177 | portlist="${ext_open_tcp// /,}" | ||
| 178 | doit $ipt -p tcp -m multiport --dports $portlist -j RETURN | ||
| 179 | fi | ||
| 180 | doit $ipt -p tcp -j REJECT # Anything else isn't ok. REJECT = irc opens faster | ||
| 181 | # (it probes proxy ports, DROP will incur timeout delays) | ||
| 182 | ipt="iptables -t filter -A INPUT" | ||
| 183 | doit $ipt -i $extif -j iext | ||
| 184 | |||
| 185 | |||
| 186 | echo; echo "* Enabling forwarding" | ||
| 187 | echo 1 >/proc/sys/net/ipv4/ip_forward | ||
| 188 | echo "/proc/sys/net/ipv4/ip_forward: `cat /proc/sys/net/ipv4/ip_forward`" | ||
| 189 | |||
| 190 | |||
| 191 | # Signal everybody that firewall is up | ||
| 192 | date '+%Y-%m-%d %H:%M:%S' >"$rundir/up" | ||
| 193 | |||
| 194 | # Ok, spew out gobs of info and disable ourself | ||
| 195 | echo; echo "* IP:" | ||
| 196 | ip a l | ||
| 197 | echo; echo "* Routing:" | ||
| 198 | ip r l | ||
| 199 | echo; echo "* Firewall:" | ||
| 200 | { | ||
| 201 | echo '---FILTER--'; | ||
| 202 | iptables -v -L -x -n; | ||
| 203 | echo '---NAT-----'; | ||
| 204 | iptables -t nat -v -L -x -n; | ||
| 205 | echo '---MANGLE--'; | ||
| 206 | iptables -t mangle -v -L -x -n; | ||
| 207 | } \ | ||
| 208 | | grep -v '^$' | grep -Fv 'bytes target' | ||
| 209 | echo | ||
| 210 | |||
| 211 | echo "* 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 | |||
| 3 | echo; echo "* Firewall:" | ||
| 4 | { | ||
| 5 | echo '---FILTER--'; | ||
| 6 | iptables -v -L -x -n; | ||
| 7 | echo '---NAT-----'; | ||
| 8 | iptables -t nat -v -L -x -n; | ||
| 9 | echo '---MANGLE--'; | ||
| 10 | iptables -t mangle -v -L -x -n; | ||
| 11 | } \ | ||
| 12 | | grep -v '^$' | grep -Fv 'bytes target' | $PAGER | ||
diff --git a/examples/var_service/ifplugd_if/ifplugd_handler b/examples/var_service/ifplugd_if/ifplugd_handler new file mode 100755 index 000000000..4962fcf98 --- /dev/null +++ b/examples/var_service/ifplugd_if/ifplugd_handler | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | # parameters: | ||
| 3 | # $1: interface | ||
| 4 | # $2: state | ||
| 5 | |||
| 6 | if test -d "/var/service/dhcp_$1"; then | ||
| 7 | if test x"$2" = x"down"; then | ||
| 8 | echo "Downing /var/service/dhcp_$1" | ||
| 9 | sv d "/var/service/dhcp_$1" | ||
| 10 | fi | ||
| 11 | if test x"$2" = x"up"; then | ||
| 12 | echo "Upping /var/service/dhcp_$1" | ||
| 13 | sv u "/var/service/dhcp_$1" | ||
| 14 | fi | ||
| 15 | fi | ||
diff --git a/examples/var_service/ifplugd_if/log/run b/examples/var_service/ifplugd_if/log/run new file mode 100755 index 000000000..560d1b19f --- /dev/null +++ b/examples/var_service/ifplugd_if/log/run | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | user=logger | ||
| 4 | |||
| 5 | logdir="/var/log/service/`(cd ..;basename $PWD)`" | ||
| 6 | mkdir -p "$logdir" 2>/dev/null | ||
| 7 | chown -R "$user": "$logdir" | ||
| 8 | chmod -R go-rwxst,u+rwX "$logdir" | ||
| 9 | rm logdir | ||
| 10 | ln -s "$logdir" logdir | ||
| 11 | |||
| 12 | # make this dir accessible to logger | ||
| 13 | chmod a+rX . | ||
| 14 | |||
| 15 | exec >/dev/null | ||
| 16 | exec 2>&1 | ||
| 17 | exec \ | ||
| 18 | env - PATH="$PATH" \ | ||
| 19 | softlimit \ | ||
| 20 | setuidgid "$user" \ | ||
| 21 | svlogd -tt "$logdir" | ||
diff --git a/examples/var_service/ifplugd_if/p_log b/examples/var_service/ifplugd_if/p_log new file mode 100755 index 000000000..a2521be05 --- /dev/null +++ b/examples/var_service/ifplugd_if/p_log | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | cd log/logdir || exit 1 | ||
| 4 | cat @* current | $PAGER | ||
diff --git a/examples/var_service/ifplugd_if/run b/examples/var_service/ifplugd_if/run new file mode 100755 index 000000000..44ddbc48d --- /dev/null +++ b/examples/var_service/ifplugd_if/run | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | exec 2>&1 | ||
| 4 | exec </dev/null | ||
| 5 | |||
| 6 | pwd="$PWD" | ||
| 7 | |||
| 8 | if="${PWD##*/ifplugd_}" | ||
| 9 | |||
| 10 | echo "* Starting ifplugd [$$]" | ||
| 11 | exec \ | ||
| 12 | env - PATH="$PATH" \ | ||
| 13 | softlimit \ | ||
| 14 | setuidgid root \ | ||
| 15 | ifplugd -apq -n -s -i "$if" -r "$pwd/ifplugd_handler" | ||
| 16 | |||
| 17 | #-n Do not daemonize | ||
| 18 | #-s Do not log to syslog | ||
| 19 | #-i IFACE Interface | ||
| 20 | #-f/-F Treat link detection error as link down/link up (otherwise exit on error) | ||
| 21 | #-a Do not up interface automatically | ||
| 22 | #-M Monitor creation/destruction of interface (otherwise it must exist) | ||
| 23 | #-r PROG Script to run | ||
| 24 | #-x ARG Extra argument for script | ||
| 25 | #-I Dont exit on nonzero exit code from script | ||
| 26 | #-p Dont run script on daemon startup | ||
| 27 | #-q Dont run script on daemon quit | ||
| 28 | #-l Run script on startup even if no cable is detected | ||
| 29 | #-t SECS Poll time in seconds | ||
| 30 | #-u SECS Delay before running script after link up | ||
| 31 | #-d SECS Delay after link down | ||
| 32 | #-m MODE API mode (mii, priv, ethtool, wlan, auto) | ||
diff --git a/examples/var_service/ifplugd_if/w_log b/examples/var_service/ifplugd_if/w_log new file mode 100755 index 000000000..34b19b373 --- /dev/null +++ b/examples/var_service/ifplugd_if/w_log | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | cd log/logdir || exit 1 | ||
| 4 | watch -n2 'w=`ttysize w`; h=`ttysize h`; tail -$((h-3)) current 2>&1 | cut -b0-$((w-2))' | ||
diff --git a/examples/var_service/nmeter/run b/examples/var_service/nmeter/run new file mode 100755 index 000000000..fa0837bc4 --- /dev/null +++ b/examples/var_service/nmeter/run | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | # Since per-process /proc/net/ (-> /proc/self/net/) appeared, | ||
| 4 | # we need to be root | ||
| 5 | user=root | ||
| 6 | tty="/dev/tty9" | ||
| 7 | |||
| 8 | chmod -R a+X . # or else env will moan | ||
| 9 | chown $user: $tty # devfs made happy | ||
| 10 | |||
| 11 | cmd="nmeter '%t %c x %x p%p f %f b %b m %m if%[nif]'" | ||
| 12 | |||
| 13 | exec >/dev/null | ||
| 14 | exec 2>&1 | ||
| 15 | exec </dev/null | ||
| 16 | |||
| 17 | eval exec \ | ||
| 18 | setuidgid "$user" \ | ||
| 19 | env - PATH="$PATH" \ | ||
| 20 | <"$tty" >"$tty" 2>&1 \ | ||
| 21 | $cmd | ||
