1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 from pymin.validation import Item, Field, UpOneOf, CIDR, Int, Invalid
4 from pymin.service.util import ListSubHandler
6 __all__ = ('FirewallHandler',)
10 r"""Rule(chain, target[, src[, dst[, ...]]]) -> Rule instance.
12 chain - INPUT, OUTPUT or FORWARD.
13 target - ACCEPT, REJECT or DROP.
14 src - Source subnet as IP/mask.
15 dst - Destination subnet as IP/mask.
16 protocol - ICMP, UDP, TCP or ALL.
17 src_port - Source port (only for UDP or TCP protocols).
18 dst_port - Destination port (only for UDP or TCP protocols).
20 chain = Field(UpOneOf(['INPUT', 'OUTPUT', 'FORWARD'], not_empty=True))
21 target = Field(UpOneOf(['ACCEPT', 'REJECT', 'DROP'], not_empty=True))
22 src = Field(CIDR(if_empty=None, if_missing=None))
23 dst = Field(CIDR(if_empty=None, if_missing=None))
24 protocol = Field(UpOneOf(['ICMP', 'UDP', 'TCP', 'ALL'], if_missing=None))
25 src_port = Field(Int(min=0, max=65535, if_empty=None, if_missing=None))
26 dst_port = Field(Int(min=0, max=65535, if_empty=None, if_missing=None))
27 def chained_validator(self, fields, state):
29 if fields['protocol'] not in ('TCP', 'UDP'):
30 for name in ('src_port', 'dst_port'):
31 if fields[name] is not None:
32 errors[name] = u"Should be None if protocol " \
33 "(%(protocol)s) is not TCP or UDP" % fields
35 raise Invalid(u"You can't specify any ports if the protocol "
36 u'is not TCP or UDP', fields, state, error_dict=errors)
38 class RuleHandler(ListSubHandler):
39 r"""RuleHandler(parent) -> RuleHandler instance :: Handle a list of rules.
41 This class is a helper for FirewallHandler to do all the work related to rules
44 parent - The parent service handler.
47 handler_help = u"Manage firewall rules"
49 _cont_subhandler_attr = 'rules'
50 _cont_subhandler_class = Rule