#!/bin/bash
-pimp=/rw/etc/pimp.conf
-etchosts=/rw/etc/hosts
+pimp_2way_nat="/rw/var/run/pimp-2way-nat.tmp"
+pimp_snat="/rw/var/run/pimp-snat.tmp"
+etchosts="/rw/etc/hosts"
-echo -n "Writing $pimp "
-echo "#This is $pimp generated by Prometheus-tools/make-pimp" > $pimp
+echo -n "Writing $pimp_2way_nat"
+echo "#This file was generated by Prometheus-tools/make-pimp" > $pimp_2way_nat
for czfip in `grep [[] $etchosts|cut -f 1`
do
pubip=`grep "^$czfip " $etchosts|cut -f 2 -d "["|cut -f 1 -d "]"`
if ! [ -z $pubip ]
then
- echo "$czfip $pubip" >> $pimp
+ echo "$czfip $pubip" >> $pimp_2way_nat
+ echo -n .
+ fi
+done
+echo " done."
+
+echo -n "Writing $pimp_snat"
+echo "#This file was generated by Prometheus-tools/make-pimp" > $pimp_snat
+for czfip in `grep "(" $etchosts|cut -f 1`
+do
+ pubip=`grep "^$czfip " $etchosts|cut -f 2 -d "("|cut -f 1 -d ")"`
+ if ! [ -z $pubip ]
+ then
+ echo "$czfip $pubip" >> $pimp_snat
echo -n .
fi
done
iptables="/sbin/iptables"
ifconfig="/sbin/ifconfig"
-#pimp.conf should be regularly updated!
-pimp="/rw/etc/pimp.conf"
+#pimp files must be generated by optional-tools/make-pimp utility
+pimp_2way_nat="/rw/var/run/pimp-2way-nat.tmp"
+pimp_snat="/rw/var/run/pimp-snat.tmp"
+etchosts="/rw/etc/hosts"
script="/rw/etc/network/snat-dnat"
echo "#!/bin/bash" > $script
echo "echo -n \"Setting firewall rules \"" >> $script
# ===============================================================
-# Symetricky SNAT-DNAT, zarazeny do indexovanych iptables
+# Symetrical SNAT-DNAT using indexed iptables
# ===============================================================
-echo -n "Generating new pimp index rules "
+echo -n "Generating new iptables rules "
-for czfip in `grep -v ^# $pimp|cut -f 1 -d " "`
+for czfip in `grep -v ^# $pimp_2way_nat|cut -f 1 -d " "`
do
- pubip=`grep "$czfip " $pimp|cut -f 2 -d " "`
+ pubip=`grep "$czfip " $pimp_2way_nat|cut -f 2 -d " "`
czffirstindex=priv_`ipcalc -n $czfip/20|grep Network|cut -f 4 -d \ |tr [./] _`
czfsecondindex=priv_`ipcalc -n $czfip/23|grep Network|cut -f 4 -d \ |tr [./] _`
czfthirdindex=priv_`ipcalc -n $czfip/26|grep Network|cut -f 4 -d \ |tr [./] _`
pubfirstindex=pub_`ipcalc -n $pubip/27|grep Network|cut -f 4 -d \ |tr [./] _`
pubsecondindex=pub_`ipcalc -n $pubip/29|grep Network|cut -f 4 -d \ |tr [./] _`
-
if ! grep $czffirstindex $script > /dev/null
then
echo $iptables -t nat -N $czffirstindex >> $script
echo " done."
# ===============================================================
-# Pravidla pro dashboard
+# SNAT only using indexed iptables (should be rather function, hmm)
+# ===============================================================
+
+for czfip in `grep -v ^# $pimp_snat|cut -f 1 -d " "`
+do
+ pubip=`grep "$czfip " $pimp_snat|cut -f 2 -d " "`
+ czffirstindex=priv_`ipcalc -n $czfip/20|grep Network|cut -f 4 -d \ |tr [./] _`
+ czfsecondindex=priv_`ipcalc -n $czfip/23|grep Network|cut -f 4 -d \ |tr [./] _`
+ czfthirdindex=priv_`ipcalc -n $czfip/26|grep Network|cut -f 4 -d \ |tr [./] _`
+
+ if ! grep $czffirstindex $script > /dev/null
+ then
+ echo $iptables -t nat -N $czffirstindex >> $script
+ echo $iptables -t nat -F $czffirstindex >> $script
+ echo $iptables -t nat -A POSTROUTING -s `ipcalc -n $czfip/20|grep Network|cut -f 4 -d \ ` -o eth1 -j $czffirstindex >> $script
+ fi
+
+ if ! grep $czfsecondindex $script > /dev/null
+ then
+ echo $iptables -t nat -N $czfsecondindex >> $script
+ echo $iptables -t nat -F $czfsecondindex >> $script
+ echo $iptables -t nat -A $czffirstindex -s `ipcalc -n $czfip/23|grep Network|cut -f 4 -d \ ` -o eth1 -j $czfsecondindex >> $script
+ fi
+
+ if ! grep $czfthirdindex $script > /dev/null
+ then
+ echo $iptables -t nat -N $czfthirdindex >> $script
+ echo $iptables -t nat -F $czfthirdindex >> $script
+ echo $iptables -t nat -A $czfsecondindex -s `ipcalc -n $czfip/26|grep Network|cut -f 4 -d \ ` -o eth1 -j $czfthirdindex >> $script
+ fi
+
+ echo $iptables -t nat -A $czfthirdindex -s $czfip/32 -o eth1 -j SNAT --to-source $pubip >> $script
+ echo $iptables -t nat -A $czfthirdindex -s $czfip/32 -o eth1 -j ACCEPT >> $script
+
+ echo -n .
+ echo "echo -n ." >>$script
+done
+echo " done."
+
+# ===============================================================
+# Dashboard rules
# ===============================================================
echo -n "Generating dashboard index rules "
-for czfip in `grep ^10[.] /etc/hosts|grep dashboard-|cut -f 1`
+for czfip in `grep ^10[.] $etchosts|grep dashboard-|cut -f 1`
do
czffirstindex=dash_`ipcalc -n $czfip/20|grep Network|cut -f 4 -d \ |tr [./] _`
czfsecondindex=dash_`ipcalc -n $czfip/23|grep Network|cut -f 4 -d \ |tr [./] _`