Files
@ 265b2706e075
Branch filter:
Location: conntrackt/conntrackt/iptables.py
265b2706e075
8.3 KiB
text/x-python
CONNT-22: Added licensing information to Python files. Added licensing information to documentation files.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Branko Majic
#
# This file is part of Django Conntrackt.
#
# Django Conntrackt is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Django Conntrackt 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.
#
# You should have received a copy of the GNU General Public License along with
# Django Conntrackt. If not, see <http://www.gnu.org/licenses/>.
#
# 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.upper() not in ("TCP", "UDP", "ICMP"):
raise ValueError("Unsupported protocol specified: %s" % protocol)
self.source = source
self.destination = destination
self.protocol = protocol.lower()
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 LoopbackRule(object):
"""
Static iptables rule that accepts all traffic on loopback interface.
"""
def __init__(self):
"""
Initialises the rule properties. Sets a static description.
"""
self.description = "Accept all incoming traffic on loopback interface."
def __unicode__(self):
"""
Creates string representation of the rule. The format is:
-i lo -j ACCEPT
Returns:
String representation of the rule.
"""
return "-i lo -j ACCEPT"
def __str__(self):
"""
Creates string representation of the rule. Calls the __unicode__
function.
Returns:
String representation of the rule.
"""
return self.__unicode__()
class RelatedRule(object):
"""
Static iptables rule that accepts all related traffic.
"""
def __init__(self):
"""
Initialises the rule properties. Sets a static description.
"""
self.description = "Accept all incoming related traffic."
def __unicode__(self):
"""
Creates string representation of the rule. The format is:
-m state --state RELATED,ESTABLISHED -j ACCEPT
Returns:
String representation of the rule.
"""
return "-m state --state RELATED,ESTABLISHED -j ACCEPT"
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 += "-A %s %s\n" % (self.name, 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__()
|