Files
@ b50fc28c1980
Branch filter:
Location: conntrackt/conntrackt/iptables.py
b50fc28c1980
6.0 KiB
text/x-python
Fixed the rendering a bit so there'd be no annoying blank lines after chain names.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | # Python imports.
from operator import attrgetter
class Rule(object):
"""
Class representing a single iptables rule. The representation does not
include the chain to which it is applied, or how it is applied (append vs
insert).
The instances of this class can be used for easily outputting a single rule.
"""
def __init__(self, source, destination, protocol, port, description=""):
"""
Initialises a rule instance. Sets-up the necessary information passed
with arguments.
Arguments:
source - Source of communication, in format IP/NETMASK.
destination - Destination of communication, in format IP/NETMASK.
protocol - Protocol used by the rule. Currently supported values are
UDP, TCP, and ICMP.
port - Destination port in a rule if protocol specified is UDP or
TCP. In case of ICMP this should be the ICMP packet type.
description - Rule description. This can be used by other classes to
produce comments in the output.
"""
if protocol not in ("TCP", "UDP", "ICMP"):
raise ValueError("Unsupported protocol specified: %s" % protocol)
self.source = source
self.destination = destination
self.protocol = protocol
self.port = port
self.description = description
def __unicode__(self):
"""
Creates string representation of the rule. The format is:
-s SOURCE -d DESTINATION -p PROTOCOL -m PROTOCOL (--dport|--icmp-type) PORT -j ACCEPT
Returns:
String representation of the rule.
"""
if self.protocol.upper() in ("TCP", "UDP"):
return "-s %s -d %s -p %s -m %s --dport %s -j ACCEPT" % (self.source, self.destination, self.protocol, self.protocol, self.port)
elif self.protocol.upper() in ("ICMP"):
return "-s %s -d %s -p %s -m %s --icmp-type %s -j ACCEPT" % (self.source, self.destination, self.protocol, self.protocol, self.port)
def __str__(self):
"""
Creates string representation of the rule. Calls the __unicode__
function.
Returns:
String representation of the rule.
"""
return self.__unicode__()
class Chain(object):
"""
Class representing a full iptables chain. Every chain has a name, default
target, and contains a number of rules.
The instances of this class can be used for easily outputting iptables rules
for a single chain.
"""
def __init__(self, name, default):
"""
Initialises a chain instance. Sets-up the necessary information passed
through the arguments.
Arguments:
name - The chain name (for example, INPUT, OUTPUT, FORWARD).
default - Default target. Currently supported values are ACCEPT, DROP,
and REJECT.
"""
if default not in ("ACCEPT", "DROP", "REJECT"):
raise ValueError("Unsupported default target specified: %s" % default)
self.name = name
self.default = default
self.rules = []
def add_rule(self, rule):
"""
Adds a new rule to the chain.
Arguments:
rule - Instance of Rule that should be added to the chain.
"""
self.rules.append(rule)
def __unicode__(self):
"""
Creates string representing of the chain. The format will be:
:NAME DEFAULT [0:0]
# DESC_RULE_1
-A NAME RULE_1
# DESC_RULE_2
-A NAME RULE_2
...
# DESC_RULE_N
-A NAME RULE_N
It should be noted that the rules will be grouped by their
description. Description line is not output if description is empty.
"""
# Set-up the "header".
rendering = ":%s %s [0:0]" % (self.name, self.default)
# Group the rules by description.
rules = list(self.rules)
rules.sort(key=attrgetter("description"))
# Use this property to figure out if we need new line separator.
previous_description=None
# Process each rule.
for rule in rules:
if rule.description != previous_description:
rendering += "\n"
if rule.description:
rendering += "# %s\n" % rule.description
previous_description = rule.description
rendering += "%s\n" % rule
rendering += "\n"
return rendering
def __str__(self):
"""
Creates string representation of the chain. Calls the __unicode__
function.
Returns:
String representation of the chain.
"""
return self.__unicode__()
class Table(object):
"""
Class representing a single iptables table (i.e. nat, or filter). Each table
has a name, and a number of associated chains.
The instances of this class can be used for easily outputting iptables rules
for a single table.
"""
def __init__(self, name):
"""
Initialises a table instance. Sets-up the necessary information passed
through the arguments.
Arguments:
name - Table name (for example, nat or filter).
"""
self.name = name
self.chains = []
def add_chain(self, chain):
"""
Adds a new chain to the table.
Arguments:
chain - Instance of Chain that should be added to the chain.
"""
self.chains.append(chain)
def __unicode__(self):
"""
Creates string representing of the table. The format will be:
*NAME
CHAIN_1
CHAIN_2
COMMIT
"""
rendering = "*%s\n" % self.name
for chain in self.chains:
rendering += "%s" % chain
rendering += "COMMIT\n"
return rendering
def __str__(self):
"""
Creates string representation of the table. Calls the __unicode__
function.
Returns:
String representation of the table.
"""
return self.__unicode__()
|