home *** CD-ROM | disk | FTP | other *** search
/ ftp.jcu.edu.au / 2014.06.ftp.jcu.edu.au.tar / ftp.jcu.edu.au / v6.3.2b / SWBD63 / fabos-6.3.2b-10.ppc.rpm / fabos-6.3.2b.10.cpio.gz / fabos-6.3.2b.10.cpio / fabos / libexec / createiptab < prev    next >
Text File  |  2010-11-10  |  10KB  |  371 lines

  1. #!/bin/sh
  2. #
  3. # Usage: createiptab.sh
  4. # Rule are defined as follows.
  5.  
  6. #    ----------         -------
  7. #    | INPUT  |-------->| TCP |------> Other protocol rules
  8. #    ----------         -------
  9. #        |
  10. #        |              -------
  11. #        -------------->| UDP |------> Other protocol rules
  12. #                       -------
  13. #
  14. # splitting rule processing in two levels makes it faster
  15. # to execute.
  16.  
  17. export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin
  18.  
  19. # update version whenever script is changed
  20.  
  21. # IPTABLES variable is not defined, hence will be null
  22. # this variable is kept if script is changed to use a 
  23. # path for iptables,  this variable can be set to proper
  24. # location
  25.  
  26. export debug_on=0
  27.  
  28. iptab_clear_rules()
  29. {
  30.     iptables -P INPUT ACCEPT
  31.     iptables -P OUTPUT ACCEPT
  32.     iptables -P FORWARD ACCEPT
  33.  
  34.     iptables -F
  35.     iptables -X
  36. }
  37.  
  38. if [ $# != 3 ]; then
  39.     echo "ERROR: $0, Incorrect number of parameters."
  40.     iptab_clear_rules;
  41.     exit 1
  42. fi
  43.  
  44. # third parameter is CP IP, script will differentiate based on CPSTATE
  45. export ACTCP_IP=$1
  46.  
  47. export CPNAME=$2
  48. export CPSTATE=$3
  49.  
  50. version=v1.0
  51.  
  52. iptables_restore=/sbin/iptables-restore
  53.  
  54. export ERRFILE="/dev/null"
  55. export INTERNAL_IFACE=eth1
  56. export INTERNAL_IFACE_BP=eth2
  57. export INET_IFACE="eth0"
  58. export DCE_IFACE="veth+"
  59. export DCE_VLAN_IFACE="vlan+"
  60. export FC="fc"
  61. export SCRDIR="/fabos/libexec"
  62. export PRIV_NET=10.0.0.0/28 
  63. export PRIV_NET_BP=127.1.0.0/16 
  64. export INTERNAL_IP1=10.0.0.5
  65. export INTERNAL_IP2=10.0.0.6
  66.  
  67. export rule_file=/tmp/netfilter.rules
  68. export proto_tcp=tcp
  69. export proto_udp=udp
  70. export proto_icmp=icmp
  71.  
  72. PROTO_TELNET=telnet
  73. PROTO_SSH=ssh
  74. PROTO_HTTP=http
  75. PROTO_APIRPC=apirpc
  76. PROTO_APIPMAP=apipmap
  77. PROTO_TCPCOMM=tcpcommon
  78. PROTO_UDPCOMM=udpcommon
  79. PRIV_NET_RULE=priv_net_rule
  80.  
  81. rule_chains="tcp udp telnet ssh http apirpc apipmap tcpcommon udpcommon"
  82.  
  83. CREATECHAINS="createchains"
  84.  
  85. echo_debug()
  86. {
  87.         echo $* 1>&2
  88. }
  89.  
  90. iptab_clean_up () 
  91. {
  92.     rm -f $rule_file
  93. }
  94.  
  95. iptab_create_header()
  96. {
  97.     # Start generating rules file
  98.  
  99.     echo "# Generated by createiptab $version" > $rule_file
  100.     echo "*filter" >> $rule_file
  101.     echo ":INPUT DROP [0:0]" >> $rule_file
  102.     echo ":FORWARD ACCEPT [0:0]" >> $rule_file
  103.     echo ":OUTPUT ACCEPT [0:0]" >> $rule_file
  104. }
  105.  
  106. iptab_sw_create_rule_chains()
  107. {
  108.     swinst=$1;
  109.  
  110.     for rule_item in $rule_chains
  111.     do 
  112.         # need this for both tcp and udp
  113.         if [ "apirpc" = "$rule_item" ]; then
  114.             echo ":$rule_item$swinst$proto_tcp - [0:0]" >> $rule_file
  115.             echo ":$rule_item$swinst$proto_udp - [0:0]" >> $rule_file
  116.             continue;
  117.         fi    
  118.  
  119.         echo ":$rule_item$swinst - [0:0]" >> $rule_file
  120.     done
  121. }
  122.  
  123. iptab_priv_net_create_rule_chain()
  124. {
  125.     echo ":$PRIV_NET_RULE - [0:0]" >> $rule_file
  126. }
  127.  
  128. iptab_cp_create_rule_chains()
  129. {
  130.     cp_code=$1;
  131.  
  132.     echo ":$proto_tcp$cp_code - [0:0]" >> $rule_file
  133.     echo ":$proto_udp$cp_code - [0:0]" >> $rule_file
  134. }
  135.  
  136. iptab_get_cp_association()
  137. {
  138.  
  139.     if [ "$ischassis" = "Yes" ] && [ "$CPSTATE" = "Standby" ]; then
  140.         ACTIVECP=0    # Dual chassis stanby CP
  141.     elif [ "$ischassis" = "Yes" ]; then
  142.         ACTIVECP=1    # Dual chassis active CP 
  143.     else
  144.         ACTIVECP=2    # Single Chassis box 
  145.     fi
  146.  
  147.     export ACTIVECP
  148. }
  149.  
  150. iptab_priv_net_put_chain_rules()
  151. {
  152.     echo "$IPTABLES -A OUTPUT -o $INTERNAL_IFACE -j $R_PRIV_NET" >> $rule_file
  153.     echo "$IPTABLES -A OUTPUT -o $INTERNAL_IFACE_BP -j $R_PRIV_NET" >> $rule_file
  154.     echo "$IPTABLES -A $R_PRIV_NET -s $PRIV_NET -d $PRIV_NET -j ACCEPT" >> $rule_file
  155.     echo "$IPTABLES -A $R_PRIV_NET -s $PRIV_NET_BP -d $PRIV_NET_BP -j ACCEPT" >> $rule_file
  156.     echo "$IPTABLES -A $R_PRIV_NET -j REJECT" >> $rule_file
  157. }
  158.  
  159. iptab_BP_priv_net_put_chain_rules()
  160. {
  161.     echo "$IPTABLES -A OUTPUT -o $INTERNAL_IFACE_BP -j $R_PRIV_NET" >> $rule_file
  162.     echo "$IPTABLES -A $R_PRIV_NET -s $PRIV_NET_BP -d $PRIV_NET_BP -j ACCEPT" >> $rule_file
  163.     echo "$IPTABLES -A $R_PRIV_NET -j REJECT" >> $rule_file
  164. }
  165.  
  166. iptab_create_common_rules()
  167. {
  168. if [ "$ACTIVECP" = "1" ]; then
  169.  
  170.     secmode0=`/fabos/libexec/secModeGet 0`
  171.     secmode1=`/fabos/libexec/secModeGet 1`
  172.     if [ "$secmode0" != "0"  -o "$secmode1" != "0" ]; then
  173.         secmode=1
  174.     else
  175.         secmode=0
  176.     fi
  177.  
  178. #    echo_debug "debug: secmode=$secmode"
  179.  
  180.     # 600 is arbitrarily chosen to allow mounts from switch
  181.     # it was observed that mount used port above 800 in most cases
  182.     echo "$IPTABLES -A $R_ACTTCP -p $proto_tcp -m $proto_tcp --dport 897 -j REJECT" >> $rule_file
  183.     if [ "$secmode" = "0" ]; then
  184.         echo "$IPTABLES -A $R_ACTTCP -p $proto_tcp -m $proto_tcp --dport 23 -j ACCEPT" >> $rule_file
  185.         echo "$IPTABLES -A $R_ACTTCP -p $proto_tcp -m $proto_tcp --dport 22 -j ACCEPT" >> $rule_file
  186.     fi
  187.     echo "$IPTABLES -A $R_ACTTCP -p $proto_tcp -m $proto_tcp --dport 600: -j ACCEPT" >> $rule_file
  188.  
  189.     echo "$IPTABLES -A $R_ACTUDP -p $proto_udp -m $proto_udp --dport 8000 -j REJECT" >> $rule_file
  190.     echo "$IPTABLES -A $R_ACTUDP -p $proto_udp -m $proto_udp --dport 9090 -j REJECT" >> $rule_file
  191.     echo "$IPTABLES -A $R_ACTUDP -p $proto_udp -m $proto_udp --dport 123 -j ACCEPT" >> $rule_file
  192.     echo "$IPTABLES -A $R_ACTUDP -p $proto_udp -m $proto_udp --dport 600: -j ACCEPT" >> $rule_file
  193.  
  194.     echo "$IPTABLES -A INPUT -p $proto_tcp -i $INET_IFACE -d $ACTCP_IP -j $R_ACTTCP" >> $rule_file
  195.     echo "$IPTABLES -A INPUT -p $proto_udp -i $INET_IFACE -d $ACTCP_IP -j $R_ACTUDP" >> $rule_file
  196.  
  197. fi # end for active CP rules
  198.  
  199. # default policy for all, eth1 should always be allowed because
  200. # two CPs talk on this. Terminator does not need eth1 rule.
  201.  
  202. echo "$IPTABLES -A INPUT -i $INET_IFACE -p $proto_icmp -m $proto_icmp -d $INTERNAL_IP1 --icmp-type 8 -j DROP" >> $rule_file
  203. echo "$IPTABLES -A INPUT -i $INET_IFACE -p $proto_icmp -m $proto_icmp -d $INTERNAL_IP2 --icmp-type 8 -j DROP" >> $rule_file
  204. echo "$IPTABLES -A INPUT -p $proto_icmp -m $proto_icmp --icmp-type 0 -j ACCEPT" >> $rule_file
  205. echo "$IPTABLES -A INPUT -p $proto_icmp -m $proto_icmp --icmp-type 8 -j ACCEPT" >> $rule_file
  206.  
  207. #
  208. # setup BP interface only on systems where it is available
  209. #
  210. /sbin/ifconfig $INTERNAL_IFACE_BP 1>$ERRFILE 2>$ERRFILE
  211. if [ $? -eq 0 ]; then
  212.     echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE_BP -j ACCEPT" >> $rule_file
  213.     echo "$IPTABLES -A FORWARD -o $INTERNAL_IFACE_BP -j REJECT" >> $rule_file
  214.     echo "$IPTABLES -A FORWARD -i $INTERNAL_IFACE_BP -j REJECT" >> $rule_file
  215. fi
  216.  
  217. #
  218. # setup to always receive anything on DCE interfaces
  219. #
  220. echo "$IPTABLES -A INPUT -i $DCE_IFACE -j ACCEPT" >> $rule_file
  221. echo "$IPTABLES -A INPUT -i $DCE_VLAN_IFACE -j ACCEPT" >> $rule_file
  222.  
  223. if [ "$ACTIVECP" != "2" ]; then
  224.     # set up forwarding rules, no forwarding from/to eth1
  225.     echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE -j ACCEPT" >> $rule_file
  226.     echo "$IPTABLES -A FORWARD -o $INTERNAL_IFACE -j REJECT" >> $rule_file
  227.     echo "$IPTABLES -A FORWARD -i $INTERNAL_IFACE -j REJECT" >> $rule_file
  228.  
  229. #    echo "$IPTABLES -A INPUT -s $PRIV_NET -j REJECT" >> $rule_file
  230. #    echo "$IPTABLES -A INPUT -d $PRIV_NET -j REJECT" >> $rule_file
  231. fi
  232.  
  233. echo "$IPTABLES -A INPUT -i lo -j ACCEPT" >> $rule_file
  234.  
  235. # this is necessary so clients do not hang waiting for response.
  236. # this was discovered with firmwaredownload
  237. echo "$IPTABLES -A INPUT -j REJECT" >> $rule_file
  238.  
  239. echo "$IPTABLES -P INPUT DROP" >> $rule_file
  240. } # iptab_create_common_rules
  241.  
  242. iptab_export_rule_names()
  243. {
  244.     MYSWITCH=$1
  245.     export R_TCP=$proto_tcp$MYSWITCH
  246.     export R_UDP=$proto_udp$MYSWITCH
  247.     export R_TELNET=$PROTO_TELNET$MYSWITCH
  248.     export R_SSH=$PROTO_SSH$MYSWITCH
  249.     export R_HTTP=$PROTO_HTTP$MYSWITCH
  250.     export R_APIRPCUDP=$PROTO_APIRPC$MYSWITCH"udp"
  251.     export R_APIRPCTCP=$PROTO_APIRPC$MYSWITCH"tcp"
  252.     export R_APIPMAP=$PROTO_APIPMAP$MYSWITCH
  253.     export R_TCPCOMMON=$PROTO_TCPCOMM$MYSWITCH
  254.     export R_UDPCOMMON=$PROTO_UDPCOMM$MYSWITCH
  255.  
  256.     export R_PRIV_NET=$PRIV_NET_RULE
  257.  
  258.     if [ "$ACTIVECP" = "1" ]; then
  259.         export R_ACTTCP="tcp"A
  260.         export R_ACTUDP="udp"A
  261.     fi
  262. }
  263.  
  264. #///////////////////////////////
  265. ################################
  266. # start of this script
  267. ################################
  268. #///////////////////////////////
  269.  
  270.  
  271. chassis_info=`getchassisconfig`
  272.  
  273. export ischassis=`echo $chassis_info | sed -n -e 's/.*Chassis based system: //gp' | \
  274.     sed -n -e 's/ .*//gp'`
  275.  
  276. export num_switches=`echo $chassis_info | sed -n -e 's/Number of switches: //gp' | \
  277.     sed -n -e 's/ .*//gp'`
  278.  
  279. iptab_create_header;
  280.  
  281. # get switch type, term/Ulysses and cp type for Ulysses
  282. iptab_get_cp_association;
  283.  
  284. # create rule chain names
  285.  
  286. # standby CP on Ulysses
  287. if [ $ACTIVECP = 0 ]; then
  288.     iptab_sw_create_rule_chains S;
  289.     iptab_priv_net_create_rule_chain;
  290.  
  291.     iptab_export_rule_names S; 
  292.     $SCRDIR/$CREATECHAINS 0;
  293.  
  294.     if [ "$?" != "0" ]; then
  295.         iptab_clear_rules;
  296.         exit 1
  297.     fi
  298.  
  299.     iptab_priv_net_put_chain_rules;
  300. fi
  301.  
  302. # Dual chassis active CP
  303. if [ $ACTIVECP = 1 ]; then
  304.     number=0
  305.     while [ $number -lt $num_switches ]; do
  306.         iptab_sw_create_rule_chains $number;
  307.         number=$((number + 1))
  308.     done
  309.  
  310.     iptab_cp_create_rule_chains A;
  311.     iptab_priv_net_create_rule_chain;
  312.  
  313.     number=0
  314.     while [ $number -lt $num_switches ]; do
  315.         iptab_export_rule_names $number; 
  316.         $SCRDIR/$CREATECHAINS $number;
  317.         if [ "$?" != "0" ]; then
  318.             iptab_clear_rules;
  319.             exit 1
  320.         fi
  321.  
  322.         number=$((number + 1))
  323.     done
  324.  
  325.     iptab_priv_net_put_chain_rules;
  326. fi
  327.  
  328. # Single chassis box
  329. if [ $ACTIVECP = 2 ]; then
  330.     iptab_sw_create_rule_chains 0;
  331.  
  332.     iptab_priv_net_create_rule_chain;
  333.  
  334.     iptab_export_rule_names 0; 
  335.     $SCRDIR/$CREATECHAINS 0;
  336.  
  337.     if [ "$?" != "0" ]; then
  338.         iptab_clear_rules;
  339.         exit 1
  340.     fi
  341.  
  342. #
  343. # setup BP interface only on systems where it is available
  344. #
  345.     /sbin/ifconfig $INTERNAL_IFACE_BP 1>$ERRFILE 2>$ERRFILE
  346.     if [ $? -eq 0 ]; then
  347.         iptab_BP_priv_net_put_chain_rules;
  348.     fi
  349.  
  350. fi
  351.  
  352. iptab_create_common_rules;
  353.  
  354. echo "COMMIT" >> $rule_file
  355. echo "# Completed" >> $rule_file
  356.  
  357. $iptables_restore < $rule_file 1> /dev/null 2>&1 
  358. if [ $? != 0 ]; then
  359.     iptab_clear_rules;
  360.     echo "ERROR: Failed to enforce new iptables rules"
  361.     exit 1
  362. fi
  363.  
  364. # add forwarding rule, let it be commented, can be uncommented when needed
  365. # if uncommented, FORWARD rule also must be changed to ACCEPT
  366. # echo "1" > /proc/sys/net/ipv4/ip_forward
  367.  
  368. # cleanup  previous state
  369. #iptab_clean_up;
  370.