1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 # TODO See if it's better (more secure) to execute commands via python instead
4 # of using script templates.
8 from pymin.seqtools import Sequence
9 from pymin.dispatcher import Handler, handler, HandlerError
10 from pymin.services.util import Restorable, ConfigWriter, ServiceHandler, \
11 TransactionalHandler, ListSubHandler
13 __ALL__ = ('FirewallHandler',)
16 r"""Rule(chain, target[, src[, dst[, ...]]]) -> Rule instance.
18 chain - INPUT, OUTPUT or FORWARD.
19 target - ACCEPT, REJECT or DROP.
20 src - Source subnet as IP/mask.
21 dst - Destination subnet as IP/mask.
22 protocol - ICMP, UDP, TCP or ALL.
23 src_port - Source port (only for UDP or TCP protocols).
24 dst_port - Destination port (only for UDP or TCP protocols).
27 def __init__(self, chain, target, src=None, dst=None, protocol=None,
28 src_port=None, dst_port=None):
29 r"Initialize object, see class documentation for details."
34 self.protocol = protocol
35 # TODO Validate that src_port and dst_port could be not None only
36 # if the protocol is UDP or TCP
37 self.src_port = src_port
38 self.dst_port = dst_port
40 def update(self, chain=None, target=None, src=None, dst=None, protocol=None,
41 src_port=None, dst_port=None):
42 r"update([chain[, ...]]) -> Update the values of a rule (see Rule doc)."
43 if chain is not None: self.chain = chain
44 if target is not None: self.target = target
45 if src is not None: self.src = src
46 if dst is not None: self.dst = dst
47 if protocol is not None: self.protocol = protocol
48 # TODO Validate that src_port and dst_port could be not None only
49 # if the protocol is UDP or TCP
50 if src_port is not None: self.src_port = src_port
51 if dst_port is not None: self.dst_port = dst_port
54 r"Return a tuple representing the rule."
55 return (self.chain, self.target, self.src, self.dst, self.protocol,
56 self.src_port, self.dst_port)
58 class RuleHandler(ListSubHandler):
59 r"""RuleHandler(parent) -> RuleHandler instance :: Handle a list of rules.
61 This class is a helper for FirewallHandler to do all the work related to rules
64 parent - The parent service handler.
67 handler_help = u"Manage firewall rules"
69 _cont_subhandler_attr = 'rules'
70 _cont_subhandler_class = Rule
72 class FirewallHandler(Restorable, ConfigWriter, ServiceHandler,
73 TransactionalHandler):
74 r"""FirewallHandler([pickle_dir[, config_dir]]) -> FirewallHandler instance.
76 Handles firewall commands using iptables.
78 pickle_dir - Directory where to write the persistent configuration data.
80 config_dir - Directory where to store de generated configuration files.
82 Both defaults to the current working directory.
85 handler_help = u"Manage firewall service"
87 _persistent_attrs = 'rules'
89 _restorable_defaults = dict(rules=list())
91 _config_writer_files = 'iptables.sh'
92 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
94 def __init__(self, pickle_dir='.', config_dir='.'):
95 r"Initialize the object, see class documentation for details."
96 self._persistent_dir = pickle_dir
97 self._config_writer_cfg_dir = config_dir
98 self._service_start = ('sh', path.join(self._config_writer_cfg_dir,
99 self._config_writer_files))
100 self._service_stop = ('iptables', '-t', 'filter', '-F')
101 self._service_restart = self._service_start
102 self._service_reload = self._service_start
103 self._config_build_templates()
105 self.rule = RuleHandler(self)
107 def _get_config_vars(self, config_file):
108 return dict(rules=self.rules)
111 if __name__ == '__main__':
115 fw_handler = FirewallHandler()
120 print fw_handler.rule.show()
125 fw_handler.rule.add('input','drop','icmp')
127 fw_handler.rule.update(0, dst='192.168.0.188/32')
129 fw_handler.rule.add('output','accept', '192.168.1.0/24')
137 os.system('rm -f *.pkl iptables.sh')