summaryrefslogtreecommitdiff
path: root/scripts/firewall/vyatta-clear-firewall
blob: 46f6b04cee0bdaa6fb8d7386e4a96f0e380a7025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
#
# Module: vyatta-clear-firewall
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2006-2009 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Mohit Mehta
# Date: February 2009
# Description: Script to clear firewall counters
#
# **** End License ****
#

print_usage()
{
    echo "Usage:"
    echo -e "\t$0 iptables/ip6tables table-name chain-name [rule-num]"
}

clear_chain() {
 sudo $iptables_cmd -t $table_name -Z $chain_name 2>/dev/null
 result=`echo $?`
 if [ $result != 0 ]; then
  echo Invalid $ip_version firewall $cli_tree chain \'$chain_name\'
  exit 1
 fi
}

clear_chain_rule() {
 result=`sudo $iptables_cmd -t $table_name -L $chain_name 2>/dev/null`
 result=`echo $?`
 if [ $result != 0 ]; then
  echo Invalid $ip_version firewall $cli_tree chain \'$chain_name\'
  exit 1
 else
  iptables_rule_num=( `sudo $iptables_cmd -t $table_name -L $chain_name \
    --line-numbers | grep "/\* $chain_name-$rule_num " | awk '{ print $1 }'` )
  num_iptables_rules=${#iptables_rule_num[*]}
  if [ $num_iptables_rules != 0 ]; then
   i=0
   while [ $i -lt $num_iptables_rules ]; do
    sudo $iptables_cmd -t $table_name -Z $chain_name ${iptables_rule_num[$i]}
    let i++
   done
  else
   echo No \'rule $rule_num\' under $ip_version firewall $cli_tree chain \'$chain_name\'
   exit 1
  fi
 fi
}

#
# main
#

if [ $# -lt 3 ]; then
 print_usage
 exit 1
fi

iptables_cmd=$1
table_name=$2
chain_name=$3
rule_num=$4
ip_version="IPv4"
cli_tree="name"

if [[ '/sbin/ip6tables' =~ $iptables_cmd ]]; then
 ip_version="IPv6"
fi

if [[ 'mangle' =~ $table_name ]]; then
 cli_tree="modify"
fi

if [ -n "$rule_num" ]; then
 clear_chain_rule
else
 clear_chain
fi

exit 0