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 / createchains < prev    next >
Text File  |  2010-11-10  |  9KB  |  359 lines

  1. #!/bin/sh
  2. #
  3. # Usage: createchains.sh
  4. #
  5. # script to enforce iptable rules
  6. # follow these steps. 
  7. # 1. First get the switch instance, Active, Standby CP status
  8. # 2. also find if CP is Active/Standby.
  9. #     for Active CP IP address block all access
  10. #     for standby CP should create union of rules for switches
  11.  
  12. # 3. on Ulysses, for switch in secure mode access is restricted,
  13. #     if other switch is in non-secure mode, access is open
  14. #     active CP access is blocked in this case and standby is open.
  15.  
  16. echo_debug()
  17. {
  18.     if [ "$debug_on" = "1" ]; then
  19.         echo $* 1>&2
  20.     fi
  21. }
  22.  
  23. iptab_create_default_rules()
  24. {
  25.     # ordering of rules is important, do not change arbitrarily
  26.  
  27.     ## TCP common rules
  28.     echo "$IPTABLES -A $R_TCPCOMMON -p $proto_tcp -m $proto_tcp --dport 897 -j REJECT" >> $rule_file
  29.     echo "$IPTABLES -A $R_TCPCOMMON -p $proto_tcp -m $proto_tcp --dport 898 -j REJECT" >> $rule_file
  30.     echo "$IPTABLES -A $R_TCPCOMMON -p $proto_tcp -m $proto_tcp --dport 600: -j ACCEPT" >> $rule_file
  31.  
  32.     # UDP common rules
  33.     echo "$IPTABLES -A $R_UDPCOMMON -p $proto_udp -m $proto_udp --dport 8000 -j REJECT" >> $rule_file
  34.     echo "$IPTABLES -A $R_UDPCOMMON -p $proto_udp -m $proto_udp --dport 9090 -j REJECT" >> $rule_file
  35.     echo "$IPTABLES -A $R_UDPCOMMON -p $proto_udp -m $proto_udp --dport 123 -j ACCEPT" >> $rule_file
  36.     echo "$IPTABLES -A $R_UDPCOMMON -p $proto_udp -m $proto_udp --dport 600: -j ACCEPT" >> $rule_file
  37.  
  38. }
  39.  
  40. iptab_create_generic_rules()
  41. {
  42.     if [ "$1" = "" ]; then
  43.         echo "ERROR: IP address is NULL"
  44.         exit 1
  45.     fi
  46.  
  47.     IP_DDR=$1 #this will be CP ip in case of CP
  48.  
  49.     # Create tcp/udp rules for ports other than specified in policies
  50.     echo "$IPTABLES -A $R_TCP -p tcp -j $R_TCPCOMMON" >> $rule_file
  51.     echo "$IPTABLES -A $R_UDP -p udp -j $R_UDPCOMMON" >> $rule_file
  52.  
  53.     echo "$IPTABLES -A INPUT -p $proto_tcp -i $INET_IFACE -d $IP_DDR -j $R_TCP" >> $rule_file
  54.     echo "$IPTABLES -A INPUT -p $proto_udp -i $INET_IFACE -d $IP_DDR -j $R_UDP" >> $rule_file
  55.  
  56.     if [ "$CPSTATE" = "Active" ]; then
  57.  
  58. #        detaching FC IP address FC interface.
  59. #        defect # 25011 - because of ARP cache corruption, this association can not be
  60. #         enforced,. ARP cache is corrupted because of Linux behaviour where it does
  61. #        not support two physical interfaces on same subnet.
  62.  
  63. #        echo "$IPTABLES -A INPUT -p $proto_tcp -i $FC_IFACE -d $FC_IP -j $R_TCP" >> $rule_file
  64. #        echo "$IPTABLES -A INPUT -p $proto_udp -i $FC_IFACE -d $FC_IP -j $R_UDP" >> $rule_file
  65.         echo "$IPTABLES -A INPUT -p $proto_tcp -d $FC_IP -j $R_TCP" >> $rule_file
  66.         echo "$IPTABLES -A INPUT -p $proto_udp -d $FC_IP -j $R_UDP" >> $rule_file
  67.     fi
  68. }
  69.  
  70. iptab_create_stdby_ruleset()
  71. {
  72.     # for one switch, no merge is needed
  73.     if [ $num_switches = 1 ]; then
  74.         cat $1 > $3
  75.         return;
  76.     fi
  77.  
  78.     # sw0 policy file policy.0.txt does not exist, exit script
  79.     if [ ! -f $1 ]; then
  80.         echo "INFO: sw0: security policy file not initialized."
  81.         exit 1
  82.     fi
  83.  
  84.     # sw1 policy file policy.1.txt does not exist, exit script
  85.     if [ ! -f $2 ]; then
  86.         echo "INFO: sw1: security policy file not initialized."
  87.         exit 1
  88.     fi
  89.  
  90.     TEMPCMD=/tmp/__sec.cmd
  91.     tmp_data_file=/tmp/__sec.data
  92.     IFS='
  93. '
  94.  
  95.     for line in `cat $1`
  96.     do
  97.         # get protocol
  98.         PROTO=${line%% *}
  99.         rem_line=${line#${PROTO} }
  100.  
  101.         # get port number
  102.         PORT=${rem_line%% *}
  103.  
  104.         rem_line=${rem_line#${PORT} }
  105.  
  106.         echo_debug "debug: rem_line=$rem_line"
  107.  
  108.         echo "grep \"$PROTO* $PORT\" $2 | sed -e 's/$PROTO* $PORT //g'" > $TEMPCMD
  109.  
  110.         chmod 755 $TEMPCMD
  111.  
  112.         iplist=`$TEMPCMD`
  113.  
  114.         if [ "$rem_line" = "ACCEPT" -o "$iplist" = "ACCEPT" ]; then
  115.             echo "$PROTO $PORT ACCEPT" >> $3
  116.         elif [ "$rem_line" = "REJECT" -a "$iplist" = "REJECT" ]; then
  117.             echo "$PROTO $PORT REJECT" >> $3
  118.         else
  119. # this command does not work for some reason, so using above workaround
  120. # writing to a file and then executing it
  121. #        iplist=`grep "$PROTO *$PORT" $2 | sed -e 's/$PROTO *$PORT //g'`
  122.  
  123.             IFS=' '
  124.             for field in $rem_line
  125.             do
  126.                 echo $field >> $tmp_data_file
  127.             done
  128.             for field in $iplist
  129.             do
  130.                 echo $field >> $tmp_data_file
  131.             done
  132.  
  133.             IFS='
  134. '
  135.             iplist=""
  136.             for field in `sort $tmp_data_file | uniq`
  137.             do
  138.                 iplist="${iplist} ${field}"
  139.             done
  140.             echo $PROTO $PORT $iplist >> $3
  141.         fi
  142.     done
  143.  
  144.     rm -f $TEMPCMD
  145.     rm -f $tmp_data_file
  146. }
  147.  
  148. iptab_create_policy_rules()
  149. {
  150.     if [ "$2" = "" ]; then
  151.         echo "ERROR: IP Address is NULL"
  152.         exit 1
  153.     fi
  154.  
  155.     echo_debug
  156.     echo_debug "debug: entering iptab_create_policy_rules"
  157.     echo_debug
  158.  
  159.     POLICYFILE=$1
  160.     echo_debug "debug: policyfile=$POLICYFILE"
  161.     DEST_IP=$2
  162.     LIMIT_RATE=20/minute
  163.     BURST_RATE=20
  164.  
  165.     # check if text policy file exists. If not, exit.
  166.     # this can happen for first time when sw0 comes up before sw1 or vice versa.
  167.     if [ ! -f $POLICYFILE ]; then
  168.         exit 1
  169.     fi
  170.  
  171.     OLD_IFS=$IFS
  172.     # make the separator as newline
  173.     IFS='
  174. '
  175.     for line in `cat $POLICYFILE`
  176.     do      
  177.     # get protocol
  178.         PROTO=${line%% *}
  179.         echo_debug debug proto: $PROTO
  180.         rem_line=${line#${PROTO} }
  181.         echo_debug debug: rem_line=$rem_line
  182.  
  183.     # get port number
  184.         PORT=${rem_line%% *}
  185.         echo_debug debug port: $PORT
  186.  
  187.         rem_line=${rem_line#${PORT} }
  188.  
  189.         echo_debug debug: rem_line=$rem_line
  190.  
  191.         case "$PORT" in
  192.             22)
  193.                 RULE=$R_SSH
  194.                 PROTOCHAIN=$R_TCP
  195.                 VIOLATED_APP="SSH"
  196.                 ;;
  197.  
  198.             23)
  199.                 RULE=$R_TELNET
  200.                 PROTOCHAIN=$R_TCP
  201.                 VIOLATED_APP="TELNET"
  202.                 ;;
  203.  
  204.             80 | 443)
  205.                 RULE=$R_HTTP
  206.                 PROTOCHAIN=$R_TCP
  207.                 VIOLATED_APP="HTTP"
  208.                 ;;
  209.  
  210.             111)
  211.                 if [ "$PROTO"  = "tcp" ]; then
  212.                 RULE=$R_APIRPCTCP
  213.                 PROTOCHAIN=$R_TCP
  214.                 else
  215.                 RULE=$R_APIRPCUDP
  216.                 PROTOCHAIN=$R_UDP
  217.                 fi
  218.                 VIOLATED_APP="API"
  219.                 ;;
  220.             897 | 898)
  221.                 RULE=$R_APIPMAP
  222.                 PROTOCHAIN=$R_TCP
  223.                 VIOLATED_APP="API"
  224.                 ;;
  225.  
  226.     # need to add ports for API
  227.  
  228.             *) 
  229.             if [ "$PROTO"  = "tcp" ]; then
  230.                 RULE=$R_TCPCOMMON
  231.             else    
  232.                 RULE=$R_UDPCOMMON
  233.             fi
  234.             ;; 
  235.         esac # end of case
  236.  
  237.         echo "$IPTABLES -A $PROTOCHAIN -p $PROTO -m $PROTO --dport $PORT -j $RULE" >> $rule_file
  238.  
  239.         IFS=' '
  240.         for IPADDR in $rem_line
  241.         do      
  242.             if [ "$IPADDR" = "REJECT" ]; then
  243.                 break;
  244.             elif [ "$PORT" = "443" -o "$PORT" = "898" ]; then
  245.             # skip, 443 enforced by 80 and 898 enforced by 897
  246.                 break;
  247.             elif [ "$IPADDR" = "ACCEPT" ]; then
  248.                 echo "$IPTABLES -A $RULE -p $PROTO -j ACCEPT" >> $rule_file
  249.                 break;
  250.             else
  251.                 echo "$IPTABLES -A $RULE -p $PROTO -s $IPADDR -j ACCEPT" >> $rule_file
  252.             fi
  253.         done # while
  254.  
  255.         IFS='
  256. '
  257.  
  258.     # if ACCEPT, do not need a violation log rule
  259.         if [ "$IPADDR" != ACCEPT ]; then
  260.             echo "$IPTABLES -A $RULE -p $PROTO -m limit --limit $LIMIT_RATE \
  261.                 --limit-burst $BURST_RATE -j ULOG --ulog-prefix \
  262.                 "\"$VIOLATED_APP violation\"" --ulog-cprange 20 --ulog-nlgroup $NLGROUP" >> $rule_file
  263.         fi
  264.  
  265.         if [ "$IPADDR" = "REJECT" ]; then
  266.             echo "$IPTABLES -A $RULE -p $PROTO -j REJECT" >> $rule_file
  267.         fi
  268.  
  269.     done   # for
  270.  
  271.     IFS=$OLD_IFS
  272.  
  273.     echo_debug
  274.     echo_debug "debug: exiting iptab_create_policy_rules"
  275.     echo_debug
  276. } # iptab_create_policy_rules
  277.  
  278. #////////////////////////////////////////
  279. ########################################
  280. # This is the start of this script
  281. ########################################
  282. #////////////////////////////////////////
  283.  
  284. export SWITCH_NO=$1;
  285.  
  286. echo_debug "debug: sw=$SWITCH_NO"
  287.  
  288. export NLGROUP=`expr $SWITCH_NO + 1`
  289.  
  290. STDBYPOLICYFILE="/tmp/stdbypolicyfile.txt"
  291.  
  292. POLICYFILE="/etc/fabos/policy."$SWITCH_NO".txt"
  293.  
  294. export FC_IFACE=$FC$SWITCH_NO
  295.  
  296. # take union of both switches for standby
  297.  
  298. # if one switch is in secure mode or policies
  299. # have not been created for it, then all access is
  300. # open on standby
  301.  
  302. if [ "$CPSTATE" = "Standby" ]; then
  303.  
  304.     # standby rules are enforced in two cases,
  305.     # 1. When CP is standby
  306.     # 2. When CP status can not be determined and default
  307.     #    is to enforce standby rules.
  308.  
  309.     SW0_PFILE="/etc/fabos/policy.0.txt"
  310.     SW1_PFILE="/etc/fabos/policy.1.txt"
  311.  
  312.     iptab_create_stdby_ruleset $SW0_PFILE $SW1_PFILE $STDBYPOLICYFILE;
  313.  
  314.     # get standby IP address
  315.     # this is a must because on the standby side, there is no SCN
  316.     # mechanism to notify IP address change, hence security IP store
  317.     # does not get updated
  318.     STDBY_IP=`ifconfig | grep "eth0" -A 1 | \
  319.         grep "inet addr" | sed -e 's/inet addr://g' | \
  320.         cut -d B -f 1 | sed -e 's/ //g'`
  321.  
  322.     iptab_create_default_rules;
  323.  
  324.     iptab_create_policy_rules $STDBYPOLICYFILE $STDBY_IP
  325.     iptab_create_generic_rules $STDBY_IP;
  326.  
  327.     rm -f $STDBYPOLICYFILE
  328.     exit 0;
  329.     
  330. else    # Active CP
  331.  
  332.     # switch IP and FC ip should not be passed in as parameter
  333.     # as script gets address for both sw0 and sw1 and parameter
  334.     # can be passed only for your switch instance. 
  335.  
  336.     if [ $SWITCH_NO = 0 ]; then
  337.         SWITCH_IP=`sed -n -e 's/ .*#sw0.*//gp' /etc/hosts`
  338.         FC_IP=`sed -n -e 's/ .*#fc0.*//gp' /etc/hosts`
  339.     fi
  340.  
  341.     if [ $SWITCH_NO = 1 ]; then
  342.         SWITCH_IP=`sed -n -e 's/ .*#sw1.*//gp' /etc/hosts`
  343.         FC_IP=`sed -n -e 's/ .*#fc1.*//gp' /etc/hosts`
  344.     fi
  345.  
  346.     iptab_create_default_rules;
  347.  
  348.     # Allow SNMP port, only on active, so can not keep in common function call
  349.     echo "$IPTABLES -A $R_UDP -p $proto_udp -m $proto_udp --dport 161 -j ACCEPT" >> $rule_file
  350.  
  351.     echo_debug "debug createtules time"
  352.     iptab_create_policy_rules $POLICYFILE $SWITCH_IP;
  353.  
  354.     iptab_create_generic_rules $SWITCH_IP;
  355.  
  356.     exit 0
  357.  
  358. fi
  359.