5 IPTABLES
=$(which iptables)
6 IP6TABLES
=$(which ip6tables)
9 function decommentcat
(){
10 sed 's/\s*#.*$//;/^\s*$/d' "$@"
13 function create_set
(){
16 if ! $IPSET list
"${set_name}" >/dev
/null
2>&1
18 echo "creating set '${set_name}'"
19 $IPSET create
"${set_name}" "$@"
23 function insert_setmatch_rules
(){
25 if [ "x$1" = "x-single-set" ]
30 local ipt set_name
="$1"
34 eval ipt
="\$IP${v}TABLES"
39 if ! $ipt -C INPUT
-m set --match-set "${set_name}${v}" src
"$@" >/dev
/null
2>&1
41 echo "initializing rule '${set_name}${v}'"
42 $ipt -I INPUT
-m set --match-set "${set_name}${v}" src
"$@"
47 function reload_cidr_sets
(){
50 # init new temporary sets
51 echo "updating set '${set_name}'"
53 create_set
"${set_name}-tmp" hash:net
54 create_set
"${set_name}6-tmp" hash:net family inet6
57 for sfx
in '' .
$(hostname -s)
59 cidrfile
="${set_name}.cidr${sfx}"
60 if [ -e "${cidrfile}" ]
62 for s
in $(decommentcat "${cidrfile}")
65 *.
*) table
="${set_name}-tmp" ;;
66 *:*) table
="${set_name}6-tmp" ;;
68 echo "unknown entry '${s}' in '${cidrfile}'" 1>&2
72 $IPSET add
"${table}" "${s}"
81 $IPSET swap
"${n}-tmp" "${n}"
82 $IPSET destroy
"${n}-tmp"
87 function add_service_entry
(){
89 port
=$(echo "${s}" | cut -d/ -f1)
90 proto
=$(echo "${s}" | cut -d/ -f2)
91 $IPSET -exist add allowed_
${proto} ${port}
94 function allow_services
(){
99 */*) add_service_entry
"${s}"
101 *) for svc
in $(getent services "${s}" | awk '{print $2}')
103 add_service_entry
"${svc}"