3
0

run 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/bin/bash
  2. # (using bashism: arrays)
  3. user="root"
  4. reset_all_netdevs=true
  5. preferred_default_route_iface="if"
  6. extif="if"
  7. ext_open_tcp="22 80 88" # space-separated
  8. # Make ourself one-shot
  9. svc -o .
  10. # Debug
  11. #date '+%Y-%m-%d %H:%M:%S' >>"$0.log"
  12. service=`basename $PWD`
  13. rundir="/var/run/service/$service"
  14. ### filter This is the default table (if no -t option is passed). It contains
  15. ### the built-in chains INPUT (for packets coming into the box itself),
  16. ### FORWARD (for packets being routed through the box), and OUTPUT (for
  17. ### locally-generated packets).
  18. ###
  19. ### nat This table is consulted when a packet that creates a new connection
  20. ### is encountered. It consists of three built-ins: PREROUTING (for
  21. ### altering packets as soon as they come in), OUTPUT (for altering
  22. ### locally-generated packets before routing), and POSTROUTING (for
  23. ### altering packets as they are about to go out).
  24. ###
  25. ### mangle It had two built-in chains: PREROUTING (for altering incoming
  26. ### packets before routing) and OUTPUT (for altering locally-generated
  27. ### packets before routing). Recently three other built-in
  28. ### chains are added: INPUT (for packets coming into the box
  29. ### itself), FORWARD (for altering packets being routed through the
  30. ### box), and POSTROUTING (for altering packets as they are about to go
  31. ### out).
  32. ###
  33. ### ...iface... ...iface...
  34. ### | ^
  35. ### v |
  36. ### -mangle,NAT- -mangle,filter- -mangle,NAT--
  37. ### |PREROUTING|-->[Routing]-->|FORWARD |-->|POSTROUTING|
  38. ### ------------ | ^ --------------- -------------
  39. ### | | ^
  40. ### | +--if NATed------------+ |
  41. ### v | |
  42. ### -mangle,filter- -mangle,NAT,filter-
  43. ### |INPUT | +->[Routing]->|OUTPUT |
  44. ### --------------- | -------------------
  45. ### | |
  46. ### v |
  47. ### ... Local Process...
  48. doit() {
  49. echo "# $*"
  50. "$@"
  51. }
  52. #exec >/dev/null
  53. exec >"$0.out"
  54. exec 2>&1
  55. exec </dev/null
  56. umask 077
  57. # Make sure rundir/ exists
  58. mkdir -p "$rundir" 2>/dev/null
  59. chown -R "$user": "$rundir"
  60. chmod -R a=rX "$rundir"
  61. rm -rf rundir 2>/dev/null
  62. ln -s "$rundir" rundir
  63. # Timestamping
  64. date '+%Y-%m-%d %H:%M:%S'
  65. echo; echo "* Reading IP config"
  66. cfg=-1
  67. # static cfg dhcp,zeroconf etc
  68. for ipconf in conf/*.ipconf "$rundir"/*.ipconf; do
  69. if test -f "$ipconf"; then
  70. echo "+ $ipconf"
  71. . "$ipconf"
  72. fi
  73. done
  74. echo; echo "* Configuring hardware"
  75. #doit ethtool -s if autoneg off speed 100 duplex full
  76. #doit ethtool -K if rx off tx off sg off tso off
  77. echo; echo "* Resetting address and routing info"
  78. if $reset_all_netdevs; then
  79. devs=`sed -n 's/ //g;s/:.*$//p' </proc/net/dev`
  80. for iface in $devs; do
  81. doit ip a f dev "$iface"
  82. doit ip r f dev "$iface" root 0/0
  83. done
  84. else
  85. doit ip a f dev lo
  86. i=0; while test "${if[$i]}"; do
  87. doit ip a f dev "${if[$i]}"
  88. doit ip r f dev "${if[$i]}" root 0/0
  89. let i++; done
  90. fi
  91. echo; echo "* Configuring addresses"
  92. doit ip a a dev lo 127.0.0.1/8 scope host
  93. doit ip a a dev lo ::1/128 scope host
  94. i=0; while test "${if[$i]}"; do
  95. if test "${ipmask[$i]}"; then
  96. doit ip a a dev "${if[$i]}" "${ipmask[$i]}" brd +
  97. doit ip l set dev "${if[$i]}" up
  98. fi
  99. let i++; done
  100. echo; echo "* Configuring routes"
  101. # If several ifaces are configured via DHCP, they often both have 0/0 route.
  102. # They have no way of knowing that this route is offered on more than one iface.
  103. # Often, it's desirable to prefer one iface: say, wired eth over wireless.
  104. # if preferred_default_route_iface is not set, 0/0 route will be assigned randomly.
  105. if test "$preferred_default_route_iface"; then
  106. i=0; while test "${if[$i]}"; do
  107. if test "${if[$i]}" = "$preferred_default_route_iface" \
  108. && test "${net[$i]}" = "0/0" \
  109. && test "${gw[$i]}"; then
  110. echo "+ default route through ${if[$i]}, ${gw[$i]}:"
  111. doit ip r a "${net[$i]}" via "${gw[$i]}"
  112. fi
  113. let i++; done
  114. fi
  115. i=0; while test "${if[$i]}"; do
  116. #echo $i:"${if[$i]}"
  117. if test "${net[$i]}" && test "${gw[$i]}"; then
  118. doit ip r a "${net[$i]}" via "${gw[$i]}"
  119. fi
  120. let i++; done
  121. echo; echo "* Recreating /etc/* files reflecting new network configuration:"
  122. for i in etc/*; do
  123. n=`basename "$i"`
  124. echo "+ $n"
  125. (. "$i") >"/etc/$n"
  126. chmod 644 "/etc/$n"
  127. done
  128. # Usage: new_chain <chain> [<table>]
  129. new_chain() {
  130. local t=""
  131. test x"$2" != x"" && t="-t $2"
  132. doit iptables $t -N $1
  133. ipt="iptables $t -A $1"
  134. }
  135. echo; echo "* Reset iptables"
  136. doit iptables --flush
  137. doit iptables --delete-chain
  138. doit iptables --zero
  139. doit iptables -t nat --flush
  140. doit iptables -t nat --delete-chain
  141. doit iptables -t nat --zero
  142. doit iptables -t mangle --flush
  143. doit iptables -t mangle --delete-chain
  144. doit iptables -t mangle --zero
  145. echo; echo "* Configure iptables"
  146. doit modprobe nf_nat_ftp
  147. doit modprobe nf_nat_tftp
  148. doit modprobe nf_conntrack_ftp
  149. doit modprobe nf_conntrack_tftp
  150. # *** nat ***
  151. # INCOMING TRAFFIC
  152. ipt="iptables -t nat -A PREROUTING"
  153. # nothing here
  154. # LOCALLY ORIGINATED TRAFFIC
  155. ipt="iptables -t nat -A OUTPUT"
  156. # nothing here
  157. # OUTGOING TRAFFIC
  158. ipt="iptables -t nat -A POSTROUTING"
  159. # Masquerade boxes on my private net
  160. doit $ipt -s 192.168.0.0/24 -o $extif -j MASQUERADE
  161. # *** mangle ***
  162. ### DEBUG
  163. ### ipt="iptables -t mangle -A PREROUTING"
  164. ### doit $ipt -s 192.168.0.0/24 -j RETURN
  165. ### ipt="iptables -t mangle -A FORWARD"
  166. ### doit $ipt -s 192.168.0.0/24 -j RETURN
  167. ### ipt="iptables -t mangle -A POSTROUTING"
  168. ### doit $ipt -s 192.168.0.0/24 -j RETURN
  169. # nothing here
  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. echo; echo "* Enabling forwarding"
  185. echo 1 >/proc/sys/net/ipv4/ip_forward
  186. echo "/proc/sys/net/ipv4/ip_forward: `cat /proc/sys/net/ipv4/ip_forward`"
  187. # Signal everybody that firewall is up
  188. date '+%Y-%m-%d %H:%M:%S' >"$rundir/up"
  189. # Ok, spew out gobs of info and disable ourself
  190. echo; echo "* IP:"
  191. ip a l
  192. echo; echo "* Routing:"
  193. ip r l
  194. echo; echo "* Firewall:"
  195. {
  196. echo '---FILTER--';
  197. iptables -v -L -x -n;
  198. echo '---NAT-----';
  199. iptables -t nat -v -L -x -n;
  200. echo '---MANGLE--';
  201. iptables -t mangle -v -L -x -n;
  202. } \
  203. | grep -v '^$' | grep -Fv 'bytes target'
  204. echo
  205. echo "* End of firewall configuration"