1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 import logging ; log = logging.getLogger('pymin.services.nat')
6 from pymin.seqtools import Sequence
7 from pymin.dispatcher import Handler, handler, HandlerError
8 from pymin.service.util import Restorable, ConfigWriter, RestartHandler, \
9 ReloadHandler, TransactionalHandler, \
10 ServiceHandler, ListSubHandler, call
12 __all__ = ('NatHandler')
15 class PortForward(Sequence):
16 r"""PortForward(dev, protocol, port, dst[, dst_port[, ...]]) -> PortForward.
18 dev - Netword device to use.
19 protocol - TCP or UDP.
20 port - Port to forward.
21 dst - Destination IP address.
22 dst_port - Destination port (at dst).
23 src_net - Source network to apply the forward (as IP/mask).
24 dst_net - Source network to apply the forward (as IP/mask).
27 def __init__(self, dev, protocol, port, dst, dst_port=None, src_net=None,
29 r"Initialize object, see class documentation for details."
32 self.protocol = protocol
35 self.dst_port = dst_port
36 self.src_net = src_net
37 self.dst_net = dst_net
39 def update(self, dev=None, protocol=None, port=None, dst=None,
40 dst_port=None, src_net=None, dst_net=None):
41 r"update([dev[, ...]]) -> Update the values of a port (see class doc)."
43 if dev is not None: self.dev = dev
44 if protocol is not None: self.protocol = protocol
45 if port is not None: self.port = port
46 if dst is not None: self.dst = dst
47 if dst_port is not None: self.dst_port = dst_port
48 if src_net is not None: self.src_net = src_net
49 if dst_net is not None: self.dst_net = dst_net
52 r"Return a tuple representing the port forward."
53 return (self.dev, self.protocol, self.port, self.dst, self.dst_port,
54 self.src_net, self.dst_net)
56 def as_call_list(self, index=None):
57 if self.dst_port is not None:
58 self.dst = self.dst + ':' + self.dst_port
59 cmd = ['-t', 'nat', '-I', 'PREROUTING']
61 cmd.append(str(index))
62 cmd.extend(('-i', self.dev, '-j', 'DNAT', '--to', self.dst,
63 '-p', self.protocol, '--dport', self.port))
64 if self.src_net is not None:
65 cmd.extend(('-s', self.src_net))
66 if self.dst_net is not None:
67 cmd.extend(('-d', self.dst_net))
70 class PortForwardHandler(ListSubHandler):
71 r"""PortForwardHandler(parent) -> PortForwardHandler instance.
73 This class is a helper for NatHandler to do all the work related to port
76 parent - The parent service handler.
79 handler_help = u"Manage NAT port forwarding."
81 _cont_subhandler_attr = 'ports'
82 _cont_subhandler_class = PortForward
85 r"""SNat(dev, src[, src_net]) -> SNat instance.
87 dev - Netword device to use.
88 src - Source IP address.
89 src_net - Source network to apply the NAT (as IP/mask).
92 def __init__(self, dev, src, src_net=None):
93 r"Initialize object, see class documentation for details."
97 self.src_net = src_net
99 def update(self, dev=None, src=None, src_net=None):
100 r"update([dev[, ...]]) -> Update the values of a snat (see class doc)."
102 if dev is not None: self.dev = dev
103 if src is not None: self.src = src
104 if src_net is not None: self.src_net = src_net
106 def __cmp__(self, other):
107 r"Compares two SNat objects."
108 return cmp(self.as_tuple(), other.as_tuple())
111 r"Return a tuple representing the snat."
112 return (self.dev, self.src, self.src_net)
114 def as_call_list(self, index=None):
115 cmd = ['-t', 'nat', '-I', 'POSTROUTING']
116 if index is not None:
117 cmd.append(str(index))
118 cmd.extend(('-o', self.dev, '-j', 'SNAT', '--to', self.src))
119 if self.src_net is not None:
120 cmd.extend(('-s', self.src_net))
123 class SNatHandler(ListSubHandler):
124 r"""SNatHandler(parent) -> SNatHandler instance.
126 This class is a helper for NatHandler to do all the work related to
129 parent - The parent service handler.
132 handler_help = u"Manage source NAT."
134 _cont_subhandler_attr = 'snats'
135 _cont_subhandler_class = SNat
137 class Masq(Sequence):
138 r"""Masq(dev, src_net) -> Masq instance.
140 dev - Netword device to use.
141 src_net - Source network to apply the masquerade (as IP/mask).
144 def __init__(self, dev, src_net):
145 r"Initialize object, see class documentation for details."
148 self.src_net = src_net
150 def update(self, dev=None, src_net=None):
151 r"update([dev[, ...]]) -> Update the values of a masq (see class doc)."
153 if dev is not None: self.dev = dev
154 if src_net is not None: self.src_net = src_net
156 def __cmp__(self, other):
157 r"Compares two Masq objects."
158 return cmp(self.as_tuple(), other.as_tuple())
161 r"Return a tuple representing the masquerade."
162 return (self.dev, self.src_net)
164 def as_call_list(self, index=None):
165 cmd = ['-t', 'nat', '-I', 'POSTROUTING']
166 if index is not None:
167 cmd.append(str(index))
168 cmd.extend(('-o', self.dev, '-j', 'MASQUERADE', '-s', self.src_net))
171 class MasqHandler(ListSubHandler):
172 r"""MasqHandler(parent) -> MasqHandler instance.
174 This class is a helper for NatHandler to do all the work related to
177 parent - The parent service handler.
180 handler_help = u"Manage NAT masquerading."
182 _cont_subhandler_attr = 'masqs'
183 _cont_subhandler_class = Masq
185 class NatHandler(Restorable, ConfigWriter, ReloadHandler, ServiceHandler,
186 TransactionalHandler):
187 r"""NatHandler([pickle_dir[, config_dir]]) -> NatHandler instance.
189 Handles NAT commands using iptables.
191 pickle_dir - Directory where to write the persistent configuration data.
193 config_dir - Directory where to store de generated configuration files.
195 Both defaults to the current working directory.
198 handler_help = u"Manage NAT (Network Address Translation) service."
200 _persistent_attrs = ('ports', 'snats', 'masqs')
202 _restorable_defaults = dict(
208 def _service_start(self):
209 log.debug(u'NatHandler._service_start(): flushing nat table')
210 call(('iptables', '-t', 'nat', '-F'))
211 for (index, port) in enumerate(self.ports):
212 log.debug(u'NatHandler._service_start: adding port %r', port)
213 call(['iptables'] + port.as_call_list(index+1))
214 for (index, snat) in enumerate(self.snats):
215 log.debug(u'NatHandler._service_start: adding snat %r', snat)
216 call(['iptables'] + snat.as_call_list(index+1))
217 for (index, masq) in enumerate(self.masqs):
218 log.debug(u'NatHandler._service_start: adding masq %r', masq)
219 call(['iptables'] + masq.as_call_list(index+1))
221 def _service_stop(self):
222 log.debug(u'NatHandler._service_stop(): flushing nat table')
223 call(('iptables', '-t', 'nat', '-F'))
225 _service_restart = _service_start
227 def __init__(self, pickle_dir='.'):
228 r"Initialize the object, see class documentation for details."
229 log.debug(u'NatHandler(%r)', pickle_dir)
230 self._persistent_dir = pickle_dir
231 ServiceHandler.__init__(self)
232 self.forward = PortForwardHandler(self)
233 self.snat = SNatHandler(self)
234 self.masq = MasqHandler(self)
237 if __name__ == '__main__':
240 level = logging.DEBUG,
241 format = '%(asctime)s %(levelname)-8s %(message)s',
242 datefmt = '%H:%M:%S',
247 handler = NatHandler()
251 print 'Forwarded ports:'
252 print handler.forward.show()
255 print handler.snat.show()
258 print handler.masq.show()
262 handler.forward.add('eth0','tcp','80', '192.168.0.9', '8080')
263 handler.forward.update(0, dst_net='192.168.0.188/32')
264 handler.forward.add('eth0', 'udp', '53', '192.168.1.0')
268 handler.snat.add('eth0', '192.168.0.9')
269 handler.snat.update(0, src_net='192.168.0.188/32')
270 handler.snat.add('eth0', '192.168.1.0')
274 handler.masq.add('eth0', '192.168.0.9/24')
275 handler.masq.update(0, src_net='192.168.0.188/30')
276 handler.masq.add('eth1', '192.168.1.0/24')
280 os.system('rm -f *.pkl')