From 3ebe9d9d573bdfa9fc9b761e6cad1c9a7bac4cd2 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Mar 2015 19:14:49 +0100 Subject: [PATCH 001/135] Make sure xt_params is set up after `import iptc`. This fixes #145. --- iptc/ip4tc.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 45d047c..4ec21c1 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -32,6 +32,9 @@ _free.restype = None _free.argtypes = [ct.POINTER(ct.c_ubyte)] +# Make sure xt_params is set up. +xtables(NFPROTO_IPV4) + def is_table_available(name): try: From 1488a4131023755358882a0112e0c6899119f06b Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Mar 2015 19:52:10 +0100 Subject: [PATCH 002/135] Remove pyflakes warnings in ip4tc.py. --- iptc/ip4tc.py | 83 +++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 45d047c..7ce0e6b 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -217,9 +217,9 @@ class iptc(object): # Check the packet `e' on chain `chain'. Returns the verdict, or # NULL and sets errno. - #iptc_check_packet = _libiptc.iptc_check_packet - #iptc_check_packet.restype = ct.c_char_p - #iptc_check_packet.argstype = [ct.c_char_p, ct.POINTER(ipt), ct.c_void_p] + # iptc_check_packet = _libiptc.iptc_check_packet + # iptc_check_packet.restype = ct.c_char_p + # iptc_check_packet.argstype = [ct.c_char_p, ct.POINTER(ipt), ct.c_void_p] # Get the number of references to this chain iptc_get_references = _libiptc.iptc_get_references @@ -537,13 +537,15 @@ def __eq__(self, match): self.match.u.user.name == match.match.u.user.name and self.match.u.user.revision == match.match.u.user.revision and self.match_buf[basesz:self.usersize] == - match.match_buf[basesz:match.usersize]): + match.match_buf[basesz:match.usersize]): return True return False def __hash__(self): - basesz = ct.sizeof(xt_entry_match) - return hash(self.match.u.match_size) ^ hash(self.match.u.user.name) ^ hash(self.match.u.user.revision) ^ hash(bytes(self.match_buf)) + return (hash(self.match.u.match_size) ^ + hash(self.match.u.user.name) ^ + hash(self.match.u.user.revision) ^ + hash(bytes(self.match_buf))) def __ne__(self, match): return not self.__eq__(match) @@ -677,19 +679,19 @@ def __init__(self, rule, name=None, target=None, revision=None): def __eq__(self, targ): basesz = ct.sizeof(xt_entry_target) if (self.target.u.target_size != targ.target.u.target_size or - self.target.u.user.name != targ.target.u.user.name or - self.target.u.user.revision != targ.target.u.user.revision): + self.target.u.user.name != targ.target.u.user.name or + self.target.u.user.revision != targ.target.u.user.revision): return False if (self.target.u.user.name == b"" or - self.target.u.user.name == b"standard" or - self.target.u.user.name == b"ACCEPT" or - self.target.u.user.name == b"DROP" or - self.target.u.user.name == b"RETURN" or - self.target.u.user.name == b"ERROR" or - self._is_standard_target()): + self.target.u.user.name == b"standard" or + self.target.u.user.name == b"ACCEPT" or + self.target.u.user.name == b"DROP" or + self.target.u.user.name == b"RETURN" or + self.target.u.user.name == b"ERROR" or + self._is_standard_target()): return True if (self._target_buf[basesz:self.usersize] == - targ._target_buf[basesz:targ.usersize]): + targ._target_buf[basesz:targ.usersize]): return True return False @@ -885,10 +887,10 @@ def __eq__(self, rule): self._matches]): return False if (self.src == rule.src and self.dst == rule.dst and - self.protocol == rule.protocol and - self.fragment == rule.fragment and - self.in_interface == rule.in_interface and - self.out_interface == rule.out_interface): + self.protocol == rule.protocol and + self.fragment == rule.fragment and + self.in_interface == rule.in_interface and + self.out_interface == rule.out_interface): return True return False @@ -1102,10 +1104,11 @@ def set_in_interface(self, intf): intf = intf[:-1] masklen -= 2 - self.entry.ip.iniface = b"".join([intf.encode(), b'\x00' * (_IFNAMSIZ - - len(intf))]) - self.entry.ip.iniface_mask = b"".join([b'\xff' * masklen, b'\x00' * - (_IFNAMSIZ - masklen)]) + self.entry.ip.iniface = b"".join([intf.encode(), + b'\x00' * (_IFNAMSIZ - len(intf))]) + self.entry.ip.iniface_mask = b"".join([b'\xff' * masklen, + b'\x00' * (_IFNAMSIZ - + masklen)]) in_interface = property(get_in_interface, set_in_interface) """This is the input network interface e.g. *eth0*. A wildcard match can @@ -1143,10 +1146,11 @@ def set_out_interface(self, intf): intf = intf[:-1] masklen -= 2 - self.entry.ip.outiface = b"".join([intf.encode(), b'\x00' * (_IFNAMSIZ - - len(intf))]) - self.entry.ip.outiface_mask = b"".join([b'\xff' * masklen, b'\x00' * - (_IFNAMSIZ - masklen)]) + self.entry.ip.outiface = b"".join([intf.encode(), + b'\x00' * (_IFNAMSIZ - len(intf))]) + self.entry.ip.outiface_mask = b"".join([b'\xff' * masklen, + b'\x00' * (_IFNAMSIZ - + masklen)]) out_interface = property(get_out_interface, set_out_interface) """This is the output network interface e.g. *eth0*. A wildcard match can @@ -1252,7 +1256,7 @@ def _set_rule(self, entry): entrysz = self._entry_size() matchsz = entry.target_offset - entrysz - #targetsz = entry.next_offset - entry.target_offset + # targetsz = entry.next_offset - entry.target_offset # iterate over matches to create blob if matchsz: @@ -1385,7 +1389,8 @@ def insert_rule(self, rule, position=0): self.table.insert_entry(self.name, rbuf, position) def replace_rule(self, rule, position=0): - """Replace existing rule in the chain at *position* with given *rule*""" + """Replace existing rule in the chain at *position* with given + *rule*""" rbuf = rule.rule if not rbuf: raise ValueError("invalid rule") @@ -1577,7 +1582,8 @@ def rename_chain(self, chain, new_name): """Rename chain *chain* to *new_name*.""" if isinstance(chain, Chain): chain = chain.name - rv = self._iptc.iptc_rename_chain(chain.encode(), new_name.encode(), self._handle) + rv = self._iptc.iptc_rename_chain(chain.encode(), new_name.encode(), + self._handle) if rv != 1: raise IPTCError("can't rename chain %s: %s" % (chain, self.strerror())) @@ -1617,7 +1623,8 @@ def set_policy(self, chain, policy, counters=None): cntrs = ct.pointer(cntrs) else: cntrs = None - rv = self._iptc.iptc_set_policy(chain.encode(), policy.encode(), cntrs, self._handle) + rv = self._iptc.iptc_set_policy(chain.encode(), policy.encode(), + cntrs, self._handle) if rv != 1: raise IPTCError("can't set policy %s on chain %s: %s)" % (policy, chain, self.strerror())) @@ -1640,7 +1647,8 @@ def get_policy(self, chain): @autocommit def append_entry(self, chain, entry): """Appends rule *entry* to *chain*.""" - rv = self._iptc.iptc_append_entry(chain.encode(), ct.cast(entry, ct.c_void_p), + rv = self._iptc.iptc_append_entry(chain.encode(), + ct.cast(entry, ct.c_void_p), self._handle) if rv != 1: raise IPTCError("can't append entry to chain %s: %s)" % @@ -1649,7 +1657,8 @@ def append_entry(self, chain, entry): @autocommit def insert_entry(self, chain, entry, position): """Inserts rule *entry* into *chain* at position *position*.""" - rv = self._iptc.iptc_insert_entry(chain.encode(), ct.cast(entry, ct.c_void_p), + rv = self._iptc.iptc_insert_entry(chain.encode(), + ct.cast(entry, ct.c_void_p), position, self._handle) if rv != 1: raise IPTCError("can't insert entry into chain %s: %s)" % @@ -1658,8 +1667,9 @@ def insert_entry(self, chain, entry, position): @autocommit def replace_entry(self, chain, entry, position): """Replace existing rule in *chain* at *position* with given *rule*.""" - rv = self._iptc.iptc_replace_entry(chain.encode(), ct.cast(entry, ct.c_void_p), - position, self._handle) + rv = self._iptc.iptc_replace_entry(chain.encode(), + ct.cast(entry, ct.c_void_p), + position, self._handle) if rv != 1: raise IPTCError("can't replace entry in chain %s: %s)" % (chain, self.strerror())) @@ -1667,7 +1677,8 @@ def replace_entry(self, chain, entry, position): @autocommit def delete_entry(self, chain, entry, mask): """Removes rule *entry* with *mask* from *chain*.""" - rv = self._iptc.iptc_delete_entry(chain.encode(), ct.cast(entry, ct.c_void_p), + rv = self._iptc.iptc_delete_entry(chain.encode(), + ct.cast(entry, ct.c_void_p), mask, self._handle) if rv != 1: raise IPTCError("can't delete entry from chain %s: %s)" % From 2e65c4ab0b4a16593bafc3f399ec499da88e93d1 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Mar 2015 19:55:38 +0100 Subject: [PATCH 003/135] Remove pyflakes warnings in ip6tc.py. --- iptc/ip6tc.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index c186845..0f9906f 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -2,7 +2,6 @@ import ctypes as ct import socket -import weakref from .ip4tc import Rule, Table, IPTCError from .util import find_library, load_kernel @@ -196,9 +195,9 @@ class ip6tc(object): # Check the packet `e' on chain `chain'. Returns the verdict, or # NULL and sets errno. - #iptc_check_packet = _libiptc.ip6tc_check_packet - #iptc_check_packet.restype = ct.c_char_p - #iptc_check_packet.argstype = [ct.c_char_p, ct.POINTER(ipt), ct.c_void_p] + # iptc_check_packet = _libiptc.ip6tc_check_packet + # iptc_check_packet.restype = ct.c_char_p + # iptc_check_packet.argstype = [ct.c_char_p, ct.POINTER(ipt), ct.c_void_p] # Get the number of references to this chain iptc_get_references = _libiptc.ip6tc_get_references @@ -246,9 +245,9 @@ def __eq__(self, rule): if x in self._matches]): return False if (self.src == rule.src and self.dst == rule.dst and - self.protocol == rule.protocol and - self.in_interface == rule.in_interface and - self.out_interface == rule.out_interface): + self.protocol == rule.protocol and + self.in_interface == rule.in_interface and + self.out_interface == rule.out_interface): return True return False @@ -445,10 +444,10 @@ def set_in_interface(self, intf): intf = intf[:-1] masklen -= 2 - self.entry.ipv6.iniface = ("".join([intf, '\x00' * (_IFNAMSIZ - - len(intf))])).encode() - self.entry.ipv6.iniface_mask = ("".join(['\x01' * masklen, '\x00' * - (_IFNAMSIZ - masklen)])).encode() + self.entry.ipv6.iniface = ("".join( + [intf, '\x00' * (_IFNAMSIZ - len(intf))])).encode() + self.entry.ipv6.iniface_mask = ("".join( + ['\x01' * masklen, '\x00' * (_IFNAMSIZ - masklen)])).encode() in_interface = property(get_in_interface, set_in_interface) """This is the input network interface e.g. *eth0*. A wildcard match can @@ -490,10 +489,10 @@ def set_out_interface(self, intf): intf = intf[:-1] masklen -= 2 - self.entry.ipv6.outiface = ("".join([intf, '\x00' * (_IFNAMSIZ - - len(intf))])).encode() - self.entry.ipv6.outiface_mask = ("".join(['\x01' * masklen, '\x00' * - (_IFNAMSIZ - masklen)])).encode() + self.entry.ipv6.outiface = ("".join( + [intf, '\x00' * (_IFNAMSIZ - len(intf))])).encode() + self.entry.ipv6.outiface_mask = ("".join( + ['\x01' * masklen, '\x00' * (_IFNAMSIZ - masklen)])).encode() out_interface = property(get_out_interface, set_out_interface) """This is the output network interface e.g. *eth0*. A wildcard match can From 072baae726f2f6642c05f04cea6a47e16d733424 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Mar 2015 19:55:48 +0100 Subject: [PATCH 004/135] Remove pyflakes warnings in xtables.py. --- iptc/xtables.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index da481aa..ce58d7b 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -23,6 +23,7 @@ XTF_TRY_LOAD = 0x02 XTF_LOAD_MUST_SUCCEED = 0x03 + XTOPT_INVERT = 1 << 0 XTOPT_MAND = 1 << 1 XTOPT_MULTI = 1 << 2 @@ -386,7 +387,8 @@ class _xtables_match_v10(ct.Structure): ("save", ct.CFUNCTYPE(None, ct.c_void_p, ct.POINTER(xt_entry_match))), # Print match name or alias - ("alias", ct.CFUNCTYPE(ct.c_char_p, ct.POINTER(xt_entry_match))), + ("alias", ct.CFUNCTYPE(ct.c_char_p, + ct.POINTER(xt_entry_match))), # pointer to list of extra command-line options ("extra_opts", ct.POINTER(option)), @@ -635,7 +637,8 @@ class _xtables_target_v10(ct.Structure): ("save", ct.CFUNCTYPE(None, ct.c_void_p, ct.POINTER(xt_entry_target))), # Print target name or alias - ("alias", ct.CFUNCTYPE(ct.c_char_p, ct.POINTER(xt_entry_target))), + ("alias", ct.CFUNCTYPE(ct.c_char_p, + ct.POINTER(xt_entry_target))), # pointer to list of extra command-line options ("extra_opts", ct.POINTER(option)), @@ -762,14 +765,14 @@ class xtables(object): _xtables_xt_params = ct.c_void_p.in_dll(_lib_xtables, "xt_params") _xtables_matches = (ct.c_void_p.in_dll(_lib_xtables, "xtables_matches")) try: - _xtables_pending_matches = (ct.c_void_p.in_dll(_lib_xtables, - "xtables_pending_matches")) + _xtables_pending_matches = (ct.c_void_p.in_dll( + _lib_xtables, "xtables_pending_matches")) except ValueError: _xtables_pending_matches = ct.POINTER(None) _xtables_targets = (ct.c_void_p.in_dll(_lib_xtables, "xtables_targets")) try: - _xtables_pending_targets = (ct.c_void_p.in_dll(_lib_xtables, - "xtables_pending_targets")) + _xtables_pending_targets = (ct.c_void_p.in_dll( + _lib_xtables, "xtables_pending_targets")) except ValueError: _xtables_pending_targets = ct.POINTER(None) From f782efaaf61fcbb2282e0b8c54255ef0b7af0c7d Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Mar 2015 19:57:15 +0100 Subject: [PATCH 005/135] Remove pyflakes warnings from test_iptc.py. --- iptc/test/test_iptc.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/iptc/test/test_iptc.py b/iptc/test/test_iptc.py index 56e7e50..03597bc 100755 --- a/iptc/test/test_iptc.py +++ b/iptc/test/test_iptc.py @@ -111,9 +111,9 @@ def test_refresh(self): def test_flush_user_chains(self): chain1 = iptc.Chain(iptc.Table(iptc.Table.FILTER), - "iptc_test_flush_chain1") + "iptc_test_flush_chain1") chain2 = iptc.Chain(iptc.Table(iptc.Table.FILTER), - "iptc_test_flush_chain2") + "iptc_test_flush_chain2") iptc.Table(iptc.Table.FILTER).create_chain(chain1) iptc.Table(iptc.Table.FILTER).create_chain(chain2) @@ -143,7 +143,8 @@ def test_flush_builtin(self): iptc.Chain(filter_table, "OUTPUT").append_rule(rule) - self.assertEquals(len(iptc.Chain(filter_table, "OUTPUT").rules), output_rule_count + 1) + self.assertEquals(len(iptc.Chain(filter_table, "OUTPUT").rules), + output_rule_count + 1) filter_table.flush() @@ -581,12 +582,12 @@ def test_rule_address(self): # valid addresses rule = iptc.Rule() for addr in [("127.0.0.1/255.255.255.0", "127.0.0.1/255.255.255.0"), - ("!127.0.0.1/255.255.255.0", "!127.0.0.1/255.255.255.0"), - ("127.0.0.1/255.255.128.0", "127.0.0.1/255.255.128.0"), - ("127.0.0.1/16", "127.0.0.1/255.255.0.0"), - ("127.0.0.1/24", "127.0.0.1/255.255.255.0"), - ("127.0.0.1/17", "127.0.0.1/255.255.128.0"), - ("!127.0.0.1/17", "!127.0.0.1/255.255.128.0")]: + ("!127.0.0.1/255.255.255.0", "!127.0.0.1/255.255.255.0"), + ("127.0.0.1/255.255.128.0", "127.0.0.1/255.255.128.0"), + ("127.0.0.1/16", "127.0.0.1/255.255.0.0"), + ("127.0.0.1/24", "127.0.0.1/255.255.255.0"), + ("127.0.0.1/17", "127.0.0.1/255.255.128.0"), + ("!127.0.0.1/17", "!127.0.0.1/255.255.128.0")]: rule.src = addr[0] self.assertEquals(rule.src, addr[1]) rule.dst = addr[0] @@ -614,7 +615,6 @@ def test_rule_address(self): else: self.fail("rule accepted invalid address %s" % (addr)) - def test_rule_interface(self): # valid interfaces rule = iptc.Rule() From e4a0ad3c7081081d6eac264c6ae2082a21d9b7e5 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 25 May 2015 15:53:22 +0200 Subject: [PATCH 006/135] Release 0.8.0. --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index a00f4fb..7e90c7a 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.8.0-dev" +__version__ = "0.8.0" From 80f052c06b6faf42235f03fcc9fad5e7c76a263f Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 25 May 2015 16:00:53 +0200 Subject: [PATCH 007/135] Start 0.9.0-dev. --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 7e90c7a..7969d4c 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.8.0" +__version__ = "0.9.0-dev" From ee5cca728fe1989ebc6be2ef2a388b326392204e Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 21 Jun 2015 23:48:34 +0200 Subject: [PATCH 008/135] Move _xt_globals to module scope. --- iptc/xtables.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index ce58d7b..7d4cbee 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -745,6 +745,15 @@ def new(*args): return new +_xt_globals = xtables_globals() +_xt_globals.option_offset = 0 +_xt_globals.program_name = version.__pkgname__.encode() +_xt_globals.program_version = version.__version__.encode() +_xt_globals.orig_opts = None +_xt_globals.opts = None +_xt_globals.exit_err = _xt_exit + + class xtables(object): _xtables_init_all = _lib_xtables.xtables_init_all _xtables_init_all.restype = ct.c_int @@ -794,13 +803,6 @@ def __new__(cls, proto): def _xtinit(self, proto, no_alias_check=False): self.proto = proto self.no_alias_check = no_alias_check - self._xt_globals = xtables_globals() - self._xt_globals.option_offset = 0 - self._xt_globals.program_name = version.__pkgname__.encode() - self._xt_globals.program_version = version.__version__.encode() - self._xt_globals.orig_opts = None - self._xt_globals.opts = None - self._xt_globals.exit_err = _xt_exit thismodule = sys.modules[__name__] matchname = "_xtables_match_v%d" % (xtables_version) @@ -821,7 +823,7 @@ def _xtinit(self, proto, no_alias_check=False): self._targets = ct.c_void_p(None).value self._pending_targets = ct.c_void_p(None).value - rv = xtables._xtables_init_all(ct.pointer(self._xt_globals), proto) + rv = xtables._xtables_init_all(ct.pointer(_xt_globals), proto) if rv: raise XTablesError("xtables_init_all() failed: %d" % (rv)) self._save_globals() From 7f52d2cf1c8b3f711f486d81176d629ad7ee087d Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 21 Jun 2015 23:50:55 +0200 Subject: [PATCH 009/135] Use module level extension cache. --- iptc/xtables.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 7d4cbee..fa756e1 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -754,6 +754,9 @@ def new(*args): _xt_globals.exit_err = _xt_exit +_loaded_exts = {} + + class xtables(object): _xtables_init_all = _lib_xtables.xtables_init_all _xtables_init_all.restype = ct.c_int @@ -814,8 +817,6 @@ def _xtinit(self, proto, no_alias_check=False): raise XTablesError("unknown xtables version %d" % (xtables_version)) - self._loaded_exts = set() - # make sure we're initializing with clean state self._xt_params = ct.c_void_p(None).value self._matches = ct.c_void_p(None).value @@ -861,14 +862,8 @@ def _check_extname(self, name): name = b"standard" return name - def _loaded(self, name): - self._loaded_exts.add(name) - - def _is_loaded(self, name): - if name in self._loaded_exts: - return True - else: - return False + def _loaded(self, name, ext): + _loaded_exts['%s___%s' % (self.proto, name)] = ext def _get_initfn_from_lib(self, name, lib): try: @@ -903,8 +898,6 @@ def _get_prefix(self): raise XTablesError("Unknown protocol %d" % (self.proto)) def _try_register(self, name): - if self._is_loaded(name): - return if isinstance(name, bytes): name = name.decode() if self._try_extinit(name, _lib_xtables): @@ -916,11 +909,20 @@ def _try_register(self, name): if self._try_extinit(name, lib): return + def _get_loaded_ext(self, name): + ext = _loaded_exts.get('%s___%s' % (self.proto, name), None) + return ext + @preserve_globals def find_match(self, name): if isinstance(name, str): name = name.encode() name = self._check_extname(name) + + ext = self._get_loaded_ext(name) + if ext is not None: + return ext + match = xtables._xtables_find_match(name, XTF_TRY_LOAD, None) if not match: self._try_register(name) @@ -929,7 +931,7 @@ def find_match(self, name): return match m = ct.cast(match, ct.POINTER(self._match_struct)) - self._loaded(m[0].name) + self._loaded(m[0].name, m) return m @preserve_globals @@ -937,6 +939,11 @@ def find_target(self, name): if isinstance(name, str): name = name.encode() name = self._check_extname(name) + + ext = self._get_loaded_ext(name) + if ext is not None: + return ext + target = xtables._xtables_find_target(name, XTF_TRY_LOAD) if not target: self._try_register(name) @@ -945,7 +952,7 @@ def find_target(self, name): return target t = ct.cast(target, ct.POINTER(self._target_struct)) - self._loaded(t[0].name) + self._loaded(t[0].name, t) return t @preserve_globals From f210f1416782a43304b2694af972e6ae77dada35 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 21 Jun 2015 23:51:28 +0200 Subject: [PATCH 010/135] Set xtables_{matches,targets} to NULL. When trying to find a match or target, explicitly set `xtables_matches` and `xtables_targets` to NULL. At the same time, cache matches and targets we have found. This way we can use protocol-independent extensions for both IPv4 and IPv6. When a match or target is not in the cache, libxtables will find it, and it will be added to the cache. This fixes #150. --- iptc/xtables.py | 65 ++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 50 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index fa756e1..fe046ef 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -731,17 +731,11 @@ def _xt_exit(status, *args): _xt_exit = _EXIT_FN(_xt_exit) -def preserve_globals(fn): +def set_nfproto(fn): def new(*args): - obj = args[0] - obj._restore_globals() - try: - ret = fn(*args) - except Exception: - obj._save_globals() - raise - obj._save_globals() - return ret + xtobj = args[0] + xtables._xtables_set_nfproto(xtobj.proto) + return fn(*args) return new @@ -817,46 +811,13 @@ def _xtinit(self, proto, no_alias_check=False): raise XTablesError("unknown xtables version %d" % (xtables_version)) - # make sure we're initializing with clean state - self._xt_params = ct.c_void_p(None).value - self._matches = ct.c_void_p(None).value - self._pending_matches = ct.c_void_p(None).value - self._targets = ct.c_void_p(None).value - self._pending_targets = ct.c_void_p(None).value - rv = xtables._xtables_init_all(ct.pointer(_xt_globals), proto) if rv: raise XTablesError("xtables_init_all() failed: %d" % (rv)) - self._save_globals() def __repr__(self): return "XTables for protocol %d" % (self.proto) - def _save_globals(self): - # Save our per-protocol libxtables global variables, and set them to - # NULL so that we don't interfere with other protocols. - null = ct.c_void_p(None) - self._xt_params = xtables._xtables_xt_params.value - xtables._xtables_xt_params.value = null.value - self._matches = xtables._xtables_matches.value - xtables._xtables_matches.value = null.value - self._pending_matches = xtables._xtables_pending_matches.value - xtables._xtables_pending_matches.value = null.value - self._targets = xtables._xtables_targets.value - xtables._xtables_targets.value = null.value - self._pending_targets = xtables._xtables_pending_targets.value - xtables._xtables_pending_targets.value = null.value - - def _restore_globals(self): - # Restore per-protocol libxtables global variables saved in - # _save_globals(). - xtables._xtables_set_nfproto(self.proto) - xtables._xtables_xt_params.value = self._xt_params - xtables._xtables_matches.value = self._matches - xtables._xtables_pending_matches.value = self._pending_matches - xtables._xtables_targets.value = self._targets - xtables._xtables_pending_targets.value = self._pending_targets - def _check_extname(self, name): if name in [b"", b"ACCEPT", b"DROP", b"QUEUE", b"RETURN"]: name = b"standard" @@ -913,7 +874,7 @@ def _get_loaded_ext(self, name): ext = _loaded_exts.get('%s___%s' % (self.proto, name), None) return ext - @preserve_globals + @set_nfproto def find_match(self, name): if isinstance(name, str): name = name.encode() @@ -923,6 +884,8 @@ def find_match(self, name): if ext is not None: return ext + xtables._xtables_matches.value = ct.c_void_p(None).value + match = xtables._xtables_find_match(name, XTF_TRY_LOAD, None) if not match: self._try_register(name) @@ -934,7 +897,7 @@ def find_match(self, name): self._loaded(m[0].name, m) return m - @preserve_globals + @set_nfproto def find_target(self, name): if isinstance(name, str): name = name.encode() @@ -944,6 +907,8 @@ def find_target(self, name): if ext is not None: return ext + xtables._xtables_targets.value = ct.c_void_p(None).value + target = xtables._xtables_find_target(name, XTF_TRY_LOAD) if not target: self._try_register(name) @@ -955,7 +920,7 @@ def find_target(self, name): self._loaded(t[0].name, t) return t - @preserve_globals + @set_nfproto def save(self, module, ip, ptr): _wrap_save(module.save, ct.cast(ct.pointer(ip), ct.c_void_p), ptr) @@ -981,7 +946,7 @@ def _parse(self, module, argv, inv, flags, entry, ptr): # Dispatch arguments to the appropriate parse function, based upon the # extension's choice of API. - @preserve_globals + @set_nfproto def parse_target(self, argv, invert, t, fw, ptr, x6_parse, x6_options): _optarg.value = len(argv) > 1 and argv[1] or None _optind.value = len(argv) - 1 @@ -1026,7 +991,7 @@ def parse_target(self, argv, invert, t, fw, ptr, x6_parse, x6_options): # Dispatch arguments to the appropriate parse function, based upon the # extension's choice of API. - @preserve_globals + @set_nfproto def parse_match(self, argv, invert, m, fw, ptr, x6_parse, x6_options): _optarg.value = len(argv) > 1 and argv[1] or None _optind.value = len(argv) - 1 @@ -1107,7 +1072,7 @@ def _fcheck_target_new(self, target): # Dispatch arguments to the appropriate final_check function, based upon # the extension's choice of API. - @preserve_globals + @set_nfproto def final_check_target(self, target): x6_fcheck = None try: @@ -1147,7 +1112,7 @@ def _fcheck_match_new(self, match): # Dispatch arguments to the appropriate final_check function, based upon # the extension's choice of API. - @preserve_globals + @set_nfproto def final_check_match(self, match): x6_fcheck = None try: From 32e7be45fd2761f517287904bb5d5bffa0862d20 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Tue, 30 Jun 2015 10:29:41 +0200 Subject: [PATCH 011/135] Set pending matches/targets to NULL. Before looking up a match/target via libxtables, set pending match/target list head to NULL. This fixes #154. --- iptc/xtables.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/iptc/xtables.py b/iptc/xtables.py index fe046ef..d21e62d 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -885,6 +885,8 @@ def find_match(self, name): return ext xtables._xtables_matches.value = ct.c_void_p(None).value + if xtables._xtables_pending_matches: + xtables._xtables_pending_matches.value = ct.c_void_p(None).value match = xtables._xtables_find_match(name, XTF_TRY_LOAD, None) if not match: @@ -908,6 +910,8 @@ def find_target(self, name): return ext xtables._xtables_targets.value = ct.c_void_p(None).value + if xtables._xtables_pending_targets: + xtables._xtables_pending_targets.value = ct.c_void_p(None).value target = xtables._xtables_find_target(name, XTF_TRY_LOAD) if not target: From 942a50dbb0e21800455d9a4853f08a1f4eecd641 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 12:44:07 +0200 Subject: [PATCH 012/135] Fix #153. --- iptc/ip4tc.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 7e1479e..9a4efd0 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -546,9 +546,21 @@ def __init__(self, rule, name=None, match=None, revision=None): if match: ct.memmove(ct.byref(self._match_buf), ct.byref(match), self.size) self._update_pointers() + alias = getattr(self._module, 'alias', None) + if alias: + self._check_alias(alias) else: self.reset() + def _check_alias(self, alias): + name = self._module.alias(self._ptr).decode() + alias_module = self._xt.find_match(name) + if alias_module is None: + return + self._alias_module = alias_module[0] + self._orig_parse = getattr(self._alias_module, 'x6_parse', None) + self._orig_options = getattr(self._alias_module, 'x6_options', None) + def __eq__(self, match): basesz = ct.sizeof(xt_entry_match) if (self.match.u.match_size == match.match.u.match_size and From 1f5a1e5a69123da26ebb47704a4cce05e40853d1 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 12:49:41 +0200 Subject: [PATCH 013/135] Move full logic to _check_alias(). --- iptc/ip4tc.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 9a4efd0..41239fd 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -546,13 +546,14 @@ def __init__(self, rule, name=None, match=None, revision=None): if match: ct.memmove(ct.byref(self._match_buf), ct.byref(match), self.size) self._update_pointers() - alias = getattr(self._module, 'alias', None) - if alias: - self._check_alias(alias) + self._check_alias() else: self.reset() - def _check_alias(self, alias): + def _check_alias(self): + alias = getattr(self._module, 'alias', None) + if not alias: + return name = self._module.alias(self._ptr).decode() alias_module = self._xt.find_match(name) if alias_module is None: From 2fe7a57e3fc9b70d0c7728cadf466063037a2c48 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 12:49:58 +0200 Subject: [PATCH 014/135] Implement _check_alias() for targets too. --- iptc/ip4tc.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 41239fd..d6dddaa 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -706,6 +706,20 @@ def __init__(self, rule, name=None, target=None, revision=None): if self._is_standard_target(): self.standard_target = name + elif target: + self._check_alias() + + def _check_alias(self): + alias = getattr(self._module, 'alias', None) + if not alias: + return + name = self._module.alias(self._ptr).decode() + alias_module = self._xt.find_target(name) + if alias_module is None: + return + self._alias_module = alias_module[0] + self._orig_parse = getattr(self._alias_module, 'x6_parse', None) + self._orig_options = getattr(self._alias_module, 'x6_options', None) def __eq__(self, targ): basesz = ct.sizeof(xt_entry_target) From 61a3fd59adf121c71a43d3e5d40bf3762a4a30fe Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 13:04:51 +0200 Subject: [PATCH 015/135] Return alias name if present. --- iptc/ip4tc.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index d6dddaa..0495ae6 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -438,6 +438,14 @@ def _update_parameters(self): for k, v in params: self.__setattr__(k, v) + def _get_alias_name(self): + if not self._module or not self._ptr: + return None + alias = getattr(self._module, 'alias', None) + if not alias: + return None + return self._module.alias(self._ptr).decode() + def __setattr__(self, name, value): if not name.startswith('_') and name not in dir(self): self.parse(name.replace("_", "-"), value) @@ -456,7 +464,8 @@ def _get_parameters(self): contain those set by the module by default too.""" def _get_name(self): - return self._name + alias = self._get_alias_name() + return alias and alias or self._name name = property(_get_name) """Name of this target or match.""" From 54a392c565b8a4537d52822ac39106270e08f955 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 13:05:10 +0200 Subject: [PATCH 016/135] Use _get_alias_name() helper in _check_alias(). --- iptc/ip4tc.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 0495ae6..2d56d4b 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -560,10 +560,9 @@ def __init__(self, rule, name=None, match=None, revision=None): self.reset() def _check_alias(self): - alias = getattr(self._module, 'alias', None) - if not alias: + name = self._get_alias_name() + if name is None: return - name = self._module.alias(self._ptr).decode() alias_module = self._xt.find_match(name) if alias_module is None: return @@ -719,10 +718,9 @@ def __init__(self, rule, name=None, target=None, revision=None): self._check_alias() def _check_alias(self): - alias = getattr(self._module, 'alias', None) - if not alias: + name = self._get_alias_name() + if name is None: return - name = self._module.alias(self._ptr).decode() alias_module = self._xt.find_target(name) if alias_module is None: return From 143fffb24c1de3254bbb5bf50f6f8cac172e52f7 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 13:05:40 +0200 Subject: [PATCH 017/135] Remove superfluous pass statements from test_matches.py. --- iptc/test/test_matches.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/iptc/test/test_matches.py b/iptc/test/test_matches.py index 0b55524..b99d9d0 100755 --- a/iptc/test/test_matches.py +++ b/iptc/test/test_matches.py @@ -366,7 +366,6 @@ def setUp(self): def tearDown(self): self.chain.flush() self.chain.delete() - pass def test_state(self): self.match.state = "RELATED,ESTABLISHED" @@ -400,7 +399,6 @@ def setUp(self): def tearDown(self): self.chain.flush() self.chain.delete() - pass def test_state(self): self.match.ctstate = "NEW,RELATED" From 88ef009e753457e08ee5154ff9c44a2e30f2ff87 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 13:06:02 +0200 Subject: [PATCH 018/135] Test if removing rule succeeds in match teardowns. --- iptc/test/test_matches.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/iptc/test/test_matches.py b/iptc/test/test_matches.py index b99d9d0..1bf86dd 100755 --- a/iptc/test/test_matches.py +++ b/iptc/test/test_matches.py @@ -93,6 +93,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -123,6 +125,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -176,6 +180,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -218,6 +224,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -268,6 +276,8 @@ def setUp(self): self.table.create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -290,6 +300,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -314,6 +326,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -364,6 +378,8 @@ def setUp(self): self.table.create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -373,7 +389,7 @@ def test_state(self): self.chain.insert_rule(self.rule) rule = self.chain.rules[0] m = rule.matches[0] - self.assertTrue(m.name, ["state", "conntrack"]) + self.assertEquals(m.name, "state") self.assertEquals(m.state, "RELATED,ESTABLISHED") @@ -397,6 +413,8 @@ def setUp(self): self.table.create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -430,6 +448,8 @@ def setUp(self): self.table.create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() From 58b97458838cd4db40073674bbad4d30780cb1fc Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 4 Jul 2015 13:08:40 +0200 Subject: [PATCH 019/135] Test if removing rule succeeds in target teardowns. --- iptc/test/test_targets.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/iptc/test/test_targets.py b/iptc/test/test_targets.py index 2262dff..3587acc 100755 --- a/iptc/test/test_targets.py +++ b/iptc/test/test_targets.py @@ -79,6 +79,8 @@ def setUp(self): iptc.Table(iptc.Table.FILTER).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -133,6 +135,8 @@ def setUp(self): iptc.Table(iptc.Table.NAT).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -181,6 +185,8 @@ def setUp(self): iptc.Table(iptc.Table.MANGLE).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -262,6 +268,8 @@ def setUp(self): iptc.Table(iptc.Table.MANGLE).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -307,6 +315,8 @@ def setUp(self): iptc.Table(iptc.Table.NAT).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -363,6 +373,8 @@ def setUp(self): iptc.Table(iptc.Table.RAW).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() @@ -393,6 +405,8 @@ def setUp(self): iptc.Table(iptc.Table.RAW).create_chain(self.chain) def tearDown(self): + for r in self.chain.rules: + self.chain.delete_rule(r) self.chain.flush() self.chain.delete() From ac68a5c945a85c54dd9b6637c443279d8e19cd7b Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 11 Jul 2015 19:53:49 +0200 Subject: [PATCH 020/135] Release 0.9.0. --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 7969d4c..6cadff1 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.9.0-dev" +__version__ = "0.9.0" From a29d91c856a583faf90ff775dc357c6b6dba03ee Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 11 Jul 2015 20:00:01 +0200 Subject: [PATCH 021/135] Start 0.10.0-dev. --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 6cadff1..68a6b1d 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.9.0" +__version__ = "0.10.0-dev" From c3dcc54a86e676cddfc927566f1dfde44008391b Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 30 Aug 2015 01:23:31 +0200 Subject: [PATCH 022/135] Cosmetics: reduce line length --- iptc/ip4tc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 2d56d4b..9738a9c 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -272,7 +272,7 @@ def set_parameter(self, parameter, value=None): @param parameter: name of the parameter to set @type parameter: C{str} - @param value: optional value of the parameter to set, defaults to C{None} + @param value: optional value of the parameter, defaults to C{None} @type value: C{str} or a C{list} of C{str} """ if value is None: From 255eac75731fce92a7eaf419b95cadc0af4dae51 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 30 Aug 2015 11:03:18 +0200 Subject: [PATCH 023/135] Provide more info when invalid rule is used --- iptc/ip4tc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 9738a9c..5cca152 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1304,7 +1304,8 @@ def _set_rule(self, entry): ct.POINTER(self._entry_type()))[0] if not isinstance(entry, self._entry_type()): - raise TypeError() + raise TypeError("Invalid rule type %s; expected %s" % + (entry, self._entry_type())) entrysz = self._entry_size() matchsz = entry.target_offset - entrysz From 78967a67250421e7f185a9879df57b20806e8e46 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 30 Aug 2015 21:19:09 +0200 Subject: [PATCH 024/135] Simplify Target._parse(). --- iptc/ip4tc.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 5cca152..62b2046 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -774,9 +774,6 @@ def _parse(self, argv, inv, entry): self._orig_parse, self._orig_options) self._target_buf = ct.cast(self._module.t, ct.POINTER(ct.c_ubyte)) if self._buffer.buffer != self._target_buf: - if self._buffer.buffer is not None: - self._buffer.buffer = None # Buffer was freed by iptables. - self._buffer = _Buffer() self._buffer.buffer = self._target_buf self._update_pointers() From 03f894ad4482afcb084f1a10c871cffc96506214 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 30 Aug 2015 21:20:52 +0200 Subject: [PATCH 025/135] Fix #157 --- iptc/ip4tc.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 62b2046..a8400de 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -548,8 +548,6 @@ def __init__(self, rule, name=None, match=None, revision=None): self._revision = revision else: self._revision = self._module.revision - if self._module.next is not None: - self._store_buffer(module) self._match_buf = (ct.c_ubyte * self.size)() if match: @@ -589,10 +587,6 @@ def __hash__(self): def __ne__(self, match): return not self.__eq__(match) - def _store_buffer(self, module): - self._buffer = _Buffer() - self._buffer.buffer = ct.cast(module, ct.POINTER(ct.c_ubyte)) - def _final_check(self): self._xt.final_check_match(self._module) From 4f4dea955dcfd754ace1718b290cdf9493647d75 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 30 Aug 2015 21:42:23 +0200 Subject: [PATCH 026/135] Use set_parameter() instead of __setattr__() In IPTCModule, certain parameters may overlap with Target/Match attribute or method names. Use set_parameter() to work around this problem. This fixes #158. --- iptc/ip4tc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index a8400de..3b6ee8a 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -436,7 +436,7 @@ def _update_parameters(self): params = self.get_all_parameters().items() self.reset() for k, v in params: - self.__setattr__(k, v) + self.set_parameter(k, v) def _get_alias_name(self): if not self._module or not self._ptr: From 69242efe794321351d5b2e9b550f78a7773569c9 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 31 Oct 2015 22:59:32 -0700 Subject: [PATCH 027/135] Release v0.10.0 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 68a6b1d..1050830 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.10.0-dev" +__version__ = "0.10.0" From e3de898a3667b8170849956b428b612b6f9d11f9 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 31 Oct 2015 23:02:38 -0700 Subject: [PATCH 028/135] Rebuild README.md --- README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1f4fc28..66e128c 100644 --- a/README.md +++ b/README.md @@ -283,17 +283,17 @@ in/out inteface etc is. To print out all rules in the FILTER table: As you see in the code snippet above, rules are organized into chains, and chains are in tables. You have a fixed set of tables; for IPv4: -- FILTER, -- NAT, -- MANGLE and -- RAW. + * FILTER, + * NAT, + * MANGLE and + * RAW. For IPv6 the tables are: -- FILTER, -- MANGLE, -- RAW and -- SECURITY. + * FILTER, + * MANGLE, + * RAW and + * SECURITY. To access a table: @@ -541,4 +541,3 @@ or more rules, than commit it: The drawback is that Table is a singleton, and if you disable autocommit, it will be disabled for all instances of that Table. - From 432eee01a329a44a2deae0b579337e7dd5067c3d Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 31 Oct 2015 23:03:31 -0700 Subject: [PATCH 029/135] Start 0.11.0-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 1050830..273e3fd 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.10.0" +__version__ = "0.11.0-dev" From 4c18f94eab93e30b941439f1ed8b5fc5b69844d3 Mon Sep 17 00:00:00 2001 From: tomislacker Date: Mon, 2 Nov 2015 14:52:56 -0600 Subject: [PATCH 030/135] Add documentation to describe 1- vs. 0-indexed lists IPTables enumerates rules with a 1-indexed list whereas Python's `list` structure is 0-indexed **Note:** This makes the assumption that libiptc will *always* return rules in an ordered fashion --- iptc/ip4tc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 3b6ee8a..ede18cb 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1465,7 +1465,12 @@ def _get_rules(self): return [self.table.create_rule(e, self) for e in entries] rules = property(_get_rules) - """This is the list of rules currently in the chain.""" + """This is the list of rules currently in the chain. + + The indexes of the Rule items produced from this list *should* correspond + to the IPTables --line-numbers value minus one. Keeping in mind that + iptables rules are 1-indexed whereas the Python list is 0-indexed + """ def autocommit(fn): From 01ec240cce3546353e6d8c470d1d05a0039064c2 Mon Sep 17 00:00:00 2001 From: tomislacker Date: Mon, 2 Nov 2015 14:53:15 -0600 Subject: [PATCH 031/135] Add test for ensuring rule enumeration from libiptc is ordered If this test passes, it allows the safe assumption that the rule index in the list returned from Chain.rules is the IPTables rule number (minus one) --- iptc/test/test_iptc.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/iptc/test/test_iptc.py b/iptc/test/test_iptc.py index 03597bc..1e33e14 100755 --- a/iptc/test/test_iptc.py +++ b/iptc/test/test_iptc.py @@ -736,6 +736,31 @@ def test_rule_iterate_mangle(self): for rule in chain.rules if rule): pass + def test_rule_iterate_rulenum(self): + """Ensure rule numbers are always returned in order""" + insert_rule_count = 3 + append_rule_count = 3 + for rule_num in range(insert_rule_count, 0, -1): + rule = iptc.Rule() + match = rule.create_match("comment") + match.comment = "rule{}".format(rule_num) + rule.create_target("ACCEPT") + self.chain.insert_rule(rule) + + for rule_num in range(append_rule_count): + rule = iptc.Rule() + match = rule.create_match("comment") + match.comment = "rule{}".format(rule_num) + rule.create_target("ACCEPT") + self.chain.append_rule(rule) + + rules = self.chain.rules + assert len(rules) == (insert_rule_count + append_rule_count) + for rule_num, rule in enumerate(rules, start=1): + assert rule.target == "ACCEPT" + assert len(rule.matches) == 1 + assert rule.matches[0].comment == "rule{}".format(rule_num) + def test_rule_insert(self): rules = [] From 605aba22e37a838180f1f4d403414cc34cab412f Mon Sep 17 00:00:00 2001 From: tomislacker Date: Mon, 2 Nov 2015 15:01:51 -0600 Subject: [PATCH 032/135] TestFix: ValueError: zero length field name in format --- iptc/test/test_iptc.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/iptc/test/test_iptc.py b/iptc/test/test_iptc.py index 1e33e14..2d4418c 100755 --- a/iptc/test/test_iptc.py +++ b/iptc/test/test_iptc.py @@ -743,14 +743,14 @@ def test_rule_iterate_rulenum(self): for rule_num in range(insert_rule_count, 0, -1): rule = iptc.Rule() match = rule.create_match("comment") - match.comment = "rule{}".format(rule_num) + match.comment = "rule{rule_num}".format(rule_num=rule_num) rule.create_target("ACCEPT") self.chain.insert_rule(rule) for rule_num in range(append_rule_count): rule = iptc.Rule() match = rule.create_match("comment") - match.comment = "rule{}".format(rule_num) + match.comment = "rule{rule_num}".format(rule_num=rule_num) rule.create_target("ACCEPT") self.chain.append_rule(rule) @@ -759,7 +759,8 @@ def test_rule_iterate_rulenum(self): for rule_num, rule in enumerate(rules, start=1): assert rule.target == "ACCEPT" assert len(rule.matches) == 1 - assert rule.matches[0].comment == "rule{}".format(rule_num) + assert rule.matches[0].comment == "rule{rule_num}".format( + rule_num=rule_num) def test_rule_insert(self): rules = [] From 491b7044ab4e708611f1fd6c7eee6da97f763245 Mon Sep 17 00:00:00 2001 From: tomislacker Date: Mon, 2 Nov 2015 15:03:59 -0600 Subject: [PATCH 033/135] BugFix: Do not check rule targets, test apparatus should have a clear chain --- iptc/test/test_iptc.py | 1 - 1 file changed, 1 deletion(-) diff --git a/iptc/test/test_iptc.py b/iptc/test/test_iptc.py index 2d4418c..4807e63 100755 --- a/iptc/test/test_iptc.py +++ b/iptc/test/test_iptc.py @@ -757,7 +757,6 @@ def test_rule_iterate_rulenum(self): rules = self.chain.rules assert len(rules) == (insert_rule_count + append_rule_count) for rule_num, rule in enumerate(rules, start=1): - assert rule.target == "ACCEPT" assert len(rule.matches) == 1 assert rule.matches[0].comment == "rule{rule_num}".format( rule_num=rule_num) From d73d28da8a72dbae4bbfc9fa83b2ef5f4c6a28d3 Mon Sep 17 00:00:00 2001 From: tomislacker Date: Mon, 2 Nov 2015 15:11:25 -0600 Subject: [PATCH 034/135] Bugfix: Bad range on rule appends --- iptc/test/test_iptc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iptc/test/test_iptc.py b/iptc/test/test_iptc.py index 4807e63..e039859 100755 --- a/iptc/test/test_iptc.py +++ b/iptc/test/test_iptc.py @@ -747,7 +747,9 @@ def test_rule_iterate_rulenum(self): rule.create_target("ACCEPT") self.chain.insert_rule(rule) - for rule_num in range(append_rule_count): + append_rulenum_start = insert_rule_count + 1 + append_rulenum_end = append_rulenum_start + 3 + for rule_num in range(append_rulenum_start, append_rulenum_end): rule = iptc.Rule() match = rule.create_match("comment") match.comment = "rule{rule_num}".format(rule_num=rule_num) From f3183d099ee28d23e4c880aa07875731b9fb6548 Mon Sep 17 00:00:00 2001 From: tomislacker Date: Mon, 2 Nov 2015 15:11:38 -0600 Subject: [PATCH 035/135] Add message for assertion failure --- iptc/test/test_iptc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/iptc/test/test_iptc.py b/iptc/test/test_iptc.py index e039859..4f73a79 100755 --- a/iptc/test/test_iptc.py +++ b/iptc/test/test_iptc.py @@ -761,7 +761,11 @@ def test_rule_iterate_rulenum(self): for rule_num, rule in enumerate(rules, start=1): assert len(rule.matches) == 1 assert rule.matches[0].comment == "rule{rule_num}".format( - rule_num=rule_num) + rule_num=rule_num), \ + "rule[{left_num}] is not new {right_num}".format( + left_num=rule_num, + right_num=rule.matches[0].comment + ) def test_rule_insert(self): rules = [] From 8529dd824b5add261ac4eb49a7245f81dc93e6d6 Mon Sep 17 00:00:00 2001 From: evilaliv3 Date: Fri, 1 Jan 2016 15:54:51 +0100 Subject: [PATCH 036/135] Properly package unit tests the python way --- .travis.yml | 2 +- README.md | 6 +----- setup.py | 4 +++- test.py | 27 --------------------------- {iptc/test => tests}/__init__.py | 0 {iptc/test => tests}/test_iptc.py | 0 {iptc/test => tests}/test_matches.py | 0 {iptc/test => tests}/test_targets.py | 0 8 files changed, 5 insertions(+), 34 deletions(-) delete mode 100755 test.py rename {iptc/test => tests}/__init__.py (100%) rename {iptc/test => tests}/test_iptc.py (100%) rename {iptc/test => tests}/test_matches.py (100%) rename {iptc/test => tests}/test_targets.py (100%) diff --git a/.travis.yml b/.travis.yml index c3f9c0e..b878ab7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,4 @@ install: - python setup.py build - python setup.py install script: - - echo "y"|sudo PATH=$PATH ./test.py + - sudo PATH=$PATH python setup.py test diff --git a/README.md b/README.md index 66e128c..39a4412 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,7 @@ installs into `/usr/local/lib`. Now you can run the tests: - % sudo PATH=$PATH ./test.py - WARNING: this test will manipulate iptables rules. - Don't do this on a production machine. - Would you like to continue? y/n y - [...] + % sudo PATH=$PATH python setup.py test The `PATH=$PATH` part is necessary after `sudo` if you have installed into a `virtualenv`, since `sudo` will reset your environment to a diff --git a/setup.py b/setup.py index ae3a46c..145f0c3 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,8 @@ """python-iptables setup script""" -from distutils.core import setup, Extension +from setuptools import setup, Extension +#from distutils.core import setup, Extension # make pyflakes happy __pkgname__ = None @@ -21,6 +22,7 @@ package_dir={"iptc": "iptc"}, ext_modules=[Extension("libxtwrapper", ["libxtwrapper/wrapper.c"])], + test_suite="tests", classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Console", diff --git a/test.py b/test.py deleted file mode 100755 index c24c956..0000000 --- a/test.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -import sys - -try: - input = raw_input -except NameError: - pass - -print("WARNING: this test will manipulate iptables rules.") -print("Don't do this on a production machine.") -while True: - print("Would you like to continue? y/n") - answer = input() - if answer in "yYnN" and len(answer) == 1: - break -if answer in "nN": - sys.exit(0) - -from iptc.test import test_iptc, test_matches, test_targets - -results = [rv for rv in [test_iptc.run_tests(), test_matches.run_tests(), - test_targets.run_tests()]] -for res in results: - if res: - sys.exit(1) diff --git a/iptc/test/__init__.py b/tests/__init__.py similarity index 100% rename from iptc/test/__init__.py rename to tests/__init__.py diff --git a/iptc/test/test_iptc.py b/tests/test_iptc.py similarity index 100% rename from iptc/test/test_iptc.py rename to tests/test_iptc.py diff --git a/iptc/test/test_matches.py b/tests/test_matches.py similarity index 100% rename from iptc/test/test_matches.py rename to tests/test_matches.py diff --git a/iptc/test/test_targets.py b/tests/test_targets.py similarity index 100% rename from iptc/test/test_targets.py rename to tests/test_targets.py From 89985ce9d474661d11331241a8003c1c2cc82cf1 Mon Sep 17 00:00:00 2001 From: evilaliv3 Date: Fri, 1 Jan 2016 16:10:06 +0100 Subject: [PATCH 037/135] Add coverage tracking during unit-tests --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b878ab7..23e2a11 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,8 @@ python: install: - python setup.py build - python setup.py install + - sudo pip install coveralls script: - - sudo PATH=$PATH python setup.py test + - sudo PATH=$PATH coverage run setup.py test +after_success: + coveralls From 4ed8ba8c6f79a44475c97a4147b7b04250eae20e Mon Sep 17 00:00:00 2001 From: evilaliv3 Date: Fri, 1 Jan 2016 16:13:34 +0100 Subject: [PATCH 038/135] Add coveralls badge to README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 39a4412..df46172 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ If you are looking for `ebtables` python bindings, check out [![Latest Release](https://pypip.in/v/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) -[![Build Status](https://travis-ci.org/ldx/python-iptables.png?branch=master)](https://travis-ci.org/ldx/python-iptables) +[![Build Status](https://travis-ci.org/ldx/python-iptables.png?branch=master)](https://travis-ci.org/ldx/python-iptables) [![Coverage Status](https://coveralls.io/repos/ldx/python-iptables/badge.svg?branch=codecoverage)](https://coveralls.io/r/ldx/python-iptables?branch=codecoverage) [![Code Health](https://landscape.io/github/ldx/python-iptables/codecoverage/landscape.svg)](https://landscape.io/github/ldx/python-iptables/codecoverage) [![Number of Downloads](https://pypip.in/d/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) From ae129da30ec282d33463863f6199f3ae175a630f Mon Sep 17 00:00:00 2001 From: James Oakley Date: Tue, 5 Jan 2016 08:02:12 -0800 Subject: [PATCH 039/135] Handle numeric protocols to allow for rules with protocols not explicitly defined in the module. --- iptc/ip4tc.py | 6 +++++- iptc/ip6tc.py | 6 +++++- tests/test_iptc.py | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index ede18cb..3c6d42a 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1218,16 +1218,20 @@ def get_protocol(self): proto = "!" else: proto = "" - proto = "".join([proto, self.protocols[self.entry.ip.proto]]) + proto = "".join([proto, self.protocols.get(self.entry.ip.proto, str(self.entry.ip.proto))]) return proto def set_protocol(self, proto): + proto = str(proto) if proto[0] == "!": self.entry.ip.invflags |= ipt_ip.IPT_INV_PROTO proto = proto[1:] else: self.entry.ip.invflags &= (~ipt_ip.IPT_INV_PROTO & ipt_ip.IPT_INV_MASK) + if proto.isdigit(): + self.entry.ip.proto = int(proto) + return for p in self.protocols.items(): if proto.lower() == p[1]: self.entry.ip.proto = p[0] diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index 0f9906f..1611525 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -503,10 +503,11 @@ def get_protocol(self): proto = "!" else: proto = "" - proto = "".join([proto, self.protocols[self.entry.ipv6.proto]]) + proto = "".join([proto, self.protocols.get(self.entry.ipv6.proto, str(self.entry.ipv6.proto))]) return proto def set_protocol(self, proto): + proto = str(proto) if proto[0] == "!": self.entry.ipv6.invflags |= ip6t_ip6.IP6T_INV_PROTO self.entry.ipv6.flags &= (~ip6t_ip6.IP6T_F_PROTO & @@ -516,6 +517,9 @@ def set_protocol(self, proto): self.entry.ipv6.invflags &= (~ip6t_ip6.IP6T_INV_PROTO & ip6t_ip6.IP6T_INV_MASK) self.entry.ipv6.flags |= ip6t_ip6.IP6T_F_PROTO + if proto.isdigit(): + self.entry.ipv6.proto = int(proto) + return for p in self.protocols.items(): if proto.lower() == p[1]: self.entry.ipv6.proto = p[0] diff --git a/tests/test_iptc.py b/tests/test_iptc.py index 4f73a79..6317d20 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -445,6 +445,13 @@ def test_rule_protocol(self): else: self.fail("rule accepted invalid protocol %s" % (proto)) + def test_rule_protocol_numeric(self): + rule = iptc.Rule6() + rule.protocol = 132 + self.assertEquals(rule.protocol, '132') + rule.protocol = '!132' + self.assertEquals(rule.protocol, '!132') + def test_rule_compare(self): r1 = iptc.Rule6() r1.src = "::1/128" @@ -668,6 +675,13 @@ def test_rule_protocol(self): else: self.fail("rule accepted invalid protocol %s" % (proto)) + def test_rule_protocol_numeric(self): + rule = iptc.Rule() + rule.protocol = 132 + self.assertEquals(rule.protocol, '132') + rule.protocol = '!132' + self.assertEquals(rule.protocol, '!132') + def test_rule_compare(self): r1 = iptc.Rule() r1.src = "127.0.0.2/255.255.255.0" From c2ccf4af77369fbd6f69d3fc32d7a4a7fbbcceb9 Mon Sep 17 00:00:00 2001 From: Jacob Cook Date: Thu, 21 Apr 2016 13:04:47 -0400 Subject: [PATCH 040/135] add xtables v11 match and target --- iptc/xtables.py | 124 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index d21e62d..b3b5be4 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -108,6 +108,10 @@ class xt_option_entry(ct.Structure): ("max", ct.c_uint)] +class xt_xlate(ct.Structure): + _fields_ = [] + + class _U1(ct.Union): _fields_ = [("match", ct.POINTER(ct.POINTER(xt_entry_match))), ("target", ct.POINTER(ct.POINTER(xt_entry_target)))] @@ -408,6 +412,62 @@ class _xtables_match_v10(ct.Structure): ("loaded", ct.c_uint)] +class _xtables_match_v11(ct.Structure): + _fields_ = [("version", ct.c_char_p), + ("next", ct.c_void_p), + ("name", ct.c_char_p), + ("real_name", ct.c_char_p), + ("revision", ct.c_uint8), + ("ext_flags", ct.c_uint8), + ("family", ct.c_uint16), + ("size", ct.c_size_t), + ("userspacesize", ct.c_size_t), + ("help", ct.CFUNCTYPE(None)), + ("init", ct.CFUNCTYPE(None, ct.POINTER(xt_entry_match))), + # fourth parameter entry is struct ipt_entry for example + # int (*parse)(int c, char **argv, int invert, unsigned int + # *flags, const void *entry, struct xt_entry_match **match) + ("parse", ct.CFUNCTYPE(ct.c_int, ct.c_int, + ct.POINTER(ct.c_char_p), ct.c_int, + ct.POINTER(ct.c_uint), ct.c_void_p, + ct.POINTER(ct.POINTER( + xt_entry_match)))), + ("final_check", ct.CFUNCTYPE(None, ct.c_uint)), + # prints out the match iff non-NULL: put space at end + # first parameter ip is struct ipt_ip * for example + ("print", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_match), ct.c_int)), + # saves the match info in parsable form to stdout. + # first parameter ip is struct ipt_ip * for example + ("save", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_match))), + # Print match name or alias + ("alias", ct.CFUNCTYPE(ct.c_char_p, + ct.POINTER(xt_entry_match))), + # pointer to list of extra command-line options + ("extra_opts", ct.POINTER(option)), + + # introduced with the new iptables API + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(xt_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(xt_fcheck_call))), + ("x6_options", ct.POINTER(xt_option_entry)), + + # Translate iptables to nft + ("xlate", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_match), + ct.POINTER(xt_xlate), ct.c_int)), + + # size of per-extension instance extra "global" scratch space + ("udata_size", ct.c_size_t), + + # ignore these men behind the curtain: + ("udata", ct.c_void_p), + ("option_offset", ct.c_uint), + ("m", ct.POINTER(xt_entry_match)), + ("mflags", ct.c_uint), + ("loaded", ct.c_uint)] + + class xtables_match(ct.Union): _fields_ = [("v1", _xtables_match_v1), ("v2", _xtables_match_v2), @@ -418,7 +478,8 @@ class xtables_match(ct.Union): ("v7", _xtables_match_v7), # Apparently v8 was skipped ("v9", _xtables_match_v9), - ("v10", _xtables_match_v10)] + ("v10", _xtables_match_v10), + ("v11", _xtables_match_v11)] class _xtables_target_v1(ct.Structure): @@ -659,6 +720,64 @@ class _xtables_target_v10(ct.Structure): ("loaded", ct.c_uint)] +class _xtables_target_v11(ct.Structure): + _fields_ = [("version", ct.c_char_p), + ("next", ct.c_void_p), + ("name", ct.c_char_p), + ("real_name", ct.c_char_p), + ("revision", ct.c_uint8), + ("ext_flags", ct.c_uint8), + ("family", ct.c_uint16), + ("size", ct.c_size_t), + ("userspacesize", ct.c_size_t), + ("help", ct.CFUNCTYPE(None)), + ("init", ct.CFUNCTYPE(None, ct.POINTER(xt_entry_target))), + # fourth parameter entry is struct ipt_entry for example + # int (*parse)(int c, char **argv, int invert, + # unsigned int *flags, const void *entry, + # struct xt_entry_target **target) + ("parse", ct.CFUNCTYPE(ct.c_int, + ct.POINTER(ct.c_char_p), ct.c_int, + ct.POINTER(ct.c_uint), ct.c_void_p, + ct.POINTER(ct.POINTER( + xt_entry_target)))), + ("final_check", ct.CFUNCTYPE(None, ct.c_uint)), + # prints out the target iff non-NULL: put space at end + # first parameter ip is struct ipt_ip * for example + ("print", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_target), ct.c_int)), + # saves the target info in parsable form to stdout. + # first parameter ip is struct ipt_ip * for example + ("save", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_target))), + # Print target name or alias + ("alias", ct.CFUNCTYPE(ct.c_char_p, + ct.POINTER(xt_entry_target))), + # pointer to list of extra command-line options + ("extra_opts", ct.POINTER(option)), + + # introduced with the new iptables API + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(xt_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(xt_fcheck_call))), + ("x6_options", ct.POINTER(xt_option_entry)), + + # Translate iptables to nft + ("xlate", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_match), + ct.POINTER(xt_xlate), ct.c_int)), + + # size of per-extension instance extra "global" scratch space + ("udata_size", ct.c_size_t), + + # ignore these men behind the curtain: + ("udata", ct.c_void_p), + ("option_offset", ct.c_uint), + ("t", ct.POINTER(xt_entry_target)), + ("tflags", ct.c_uint), + ("used", ct.c_uint), + ("loaded", ct.c_uint)] + + class xtables_target(ct.Union): _fields_ = [("v1", _xtables_target_v1), ("v2", _xtables_target_v2), @@ -669,7 +788,8 @@ class xtables_target(ct.Union): ("v7", _xtables_target_v7), # Apparently v8 was skipped ("v9", _xtables_target_v9), - ("v10", _xtables_target_v10)] + ("v10", _xtables_target_v10), + ("v11", _xtables_target_v11)] class XTablesError(Exception): From fdae2efb101e03ff347e33ccffe073432b3f2703 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 29 Apr 2016 07:44:05 -0400 Subject: [PATCH 041/135] Prevent Segfault on v11 xtables This builds on top of proposed #173 but fixes the segfault issue when interacting with xtables. The structure of the tables did not change. Closes #172. --- iptc/xtables.py | 112 +----------------------------------------------- 1 file changed, 2 insertions(+), 110 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index b3b5be4..f80f4bd 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -412,60 +412,7 @@ class _xtables_match_v10(ct.Structure): ("loaded", ct.c_uint)] -class _xtables_match_v11(ct.Structure): - _fields_ = [("version", ct.c_char_p), - ("next", ct.c_void_p), - ("name", ct.c_char_p), - ("real_name", ct.c_char_p), - ("revision", ct.c_uint8), - ("ext_flags", ct.c_uint8), - ("family", ct.c_uint16), - ("size", ct.c_size_t), - ("userspacesize", ct.c_size_t), - ("help", ct.CFUNCTYPE(None)), - ("init", ct.CFUNCTYPE(None, ct.POINTER(xt_entry_match))), - # fourth parameter entry is struct ipt_entry for example - # int (*parse)(int c, char **argv, int invert, unsigned int - # *flags, const void *entry, struct xt_entry_match **match) - ("parse", ct.CFUNCTYPE(ct.c_int, ct.c_int, - ct.POINTER(ct.c_char_p), ct.c_int, - ct.POINTER(ct.c_uint), ct.c_void_p, - ct.POINTER(ct.POINTER( - xt_entry_match)))), - ("final_check", ct.CFUNCTYPE(None, ct.c_uint)), - # prints out the match iff non-NULL: put space at end - # first parameter ip is struct ipt_ip * for example - ("print", ct.CFUNCTYPE(None, ct.c_void_p, - ct.POINTER(xt_entry_match), ct.c_int)), - # saves the match info in parsable form to stdout. - # first parameter ip is struct ipt_ip * for example - ("save", ct.CFUNCTYPE(None, ct.c_void_p, - ct.POINTER(xt_entry_match))), - # Print match name or alias - ("alias", ct.CFUNCTYPE(ct.c_char_p, - ct.POINTER(xt_entry_match))), - # pointer to list of extra command-line options - ("extra_opts", ct.POINTER(option)), - - # introduced with the new iptables API - ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(xt_option_call))), - ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(xt_fcheck_call))), - ("x6_options", ct.POINTER(xt_option_entry)), - - # Translate iptables to nft - ("xlate", ct.CFUNCTYPE(None, ct.c_void_p, - ct.POINTER(xt_entry_match), - ct.POINTER(xt_xlate), ct.c_int)), - - # size of per-extension instance extra "global" scratch space - ("udata_size", ct.c_size_t), - - # ignore these men behind the curtain: - ("udata", ct.c_void_p), - ("option_offset", ct.c_uint), - ("m", ct.POINTER(xt_entry_match)), - ("mflags", ct.c_uint), - ("loaded", ct.c_uint)] +_xtables_match_v11 = _xtables_match_v10 class xtables_match(ct.Union): @@ -720,62 +667,7 @@ class _xtables_target_v10(ct.Structure): ("loaded", ct.c_uint)] -class _xtables_target_v11(ct.Structure): - _fields_ = [("version", ct.c_char_p), - ("next", ct.c_void_p), - ("name", ct.c_char_p), - ("real_name", ct.c_char_p), - ("revision", ct.c_uint8), - ("ext_flags", ct.c_uint8), - ("family", ct.c_uint16), - ("size", ct.c_size_t), - ("userspacesize", ct.c_size_t), - ("help", ct.CFUNCTYPE(None)), - ("init", ct.CFUNCTYPE(None, ct.POINTER(xt_entry_target))), - # fourth parameter entry is struct ipt_entry for example - # int (*parse)(int c, char **argv, int invert, - # unsigned int *flags, const void *entry, - # struct xt_entry_target **target) - ("parse", ct.CFUNCTYPE(ct.c_int, - ct.POINTER(ct.c_char_p), ct.c_int, - ct.POINTER(ct.c_uint), ct.c_void_p, - ct.POINTER(ct.POINTER( - xt_entry_target)))), - ("final_check", ct.CFUNCTYPE(None, ct.c_uint)), - # prints out the target iff non-NULL: put space at end - # first parameter ip is struct ipt_ip * for example - ("print", ct.CFUNCTYPE(None, ct.c_void_p, - ct.POINTER(xt_entry_target), ct.c_int)), - # saves the target info in parsable form to stdout. - # first parameter ip is struct ipt_ip * for example - ("save", ct.CFUNCTYPE(None, ct.c_void_p, - ct.POINTER(xt_entry_target))), - # Print target name or alias - ("alias", ct.CFUNCTYPE(ct.c_char_p, - ct.POINTER(xt_entry_target))), - # pointer to list of extra command-line options - ("extra_opts", ct.POINTER(option)), - - # introduced with the new iptables API - ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(xt_option_call))), - ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(xt_fcheck_call))), - ("x6_options", ct.POINTER(xt_option_entry)), - - # Translate iptables to nft - ("xlate", ct.CFUNCTYPE(None, ct.c_void_p, - ct.POINTER(xt_entry_match), - ct.POINTER(xt_xlate), ct.c_int)), - - # size of per-extension instance extra "global" scratch space - ("udata_size", ct.c_size_t), - - # ignore these men behind the curtain: - ("udata", ct.c_void_p), - ("option_offset", ct.c_uint), - ("t", ct.POINTER(xt_entry_target)), - ("tflags", ct.c_uint), - ("used", ct.c_uint), - ("loaded", ct.c_uint)] +_xtables_target_v11 = _xtables_target_v10 class xtables_target(ct.Union): From 942a6b955cb0eaca74878562d15ab8b8e61428bf Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 12 May 2016 14:48:06 -0400 Subject: [PATCH 042/135] Add New 'compat_rev' Member to 'xtables_globals' Library verisons greater than 10 have an added member in the xtables_global struct. --- iptc/xtables.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index f80f4bd..8537338 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -84,7 +84,9 @@ class xtables_globals(ct.Structure): ("program_version", ct.c_char_p), ("orig_opts", ct.c_void_p), ("opts", ct.c_void_p), - ("exit_err", ct.CFUNCTYPE(None, ct.c_int, ct.c_char_p))] + ("exit_err", ct.CFUNCTYPE(None, ct.c_int, ct.c_char_p)), + ("compat_rev", ct.CFUNCTYPE(ct.c_int, ct.c_char_p, ct.c_uint8, + ct.c_int))] # struct used by getopt() @@ -759,6 +761,11 @@ def new(*args): _xt_globals.opts = None _xt_globals.exit_err = _xt_exit +if xtables_version > 10: + _COMPAT_REV_FN = ct.CFUNCTYPE(ct.c_int, ct.c_char_p, ct.c_uint8, ct.c_int) + _xt_compat_rev = _COMPAT_REV_FN(_lib_xtables.xtables_compatible_revision) + _xt_globals.compat_rev = _xt_compat_rev + _loaded_exts = {} From 4fd349494bfe7638191bc1596a57068074bb9218 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 12 May 2016 19:52:34 -0400 Subject: [PATCH 043/135] Remove Now Unused 'xt_xlate' --- iptc/xtables.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 8537338..2cfccbb 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -110,10 +110,6 @@ class xt_option_entry(ct.Structure): ("max", ct.c_uint)] -class xt_xlate(ct.Structure): - _fields_ = [] - - class _U1(ct.Union): _fields_ = [("match", ct.POINTER(ct.POINTER(xt_entry_match))), ("target", ct.POINTER(ct.POINTER(xt_entry_target)))] From 2b4d08f973cfae724064e159a8120fd118f34d5d Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 23 May 2016 22:09:27 -0700 Subject: [PATCH 044/135] Release 0.11.0 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 273e3fd..e379bfe 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.11.0-dev" +__version__ = "0.11.0" From 21e40fa2bb30806983d8cd74a203ddf9f7394b85 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 23 May 2016 22:12:41 -0700 Subject: [PATCH 045/135] Start 0.12.0-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index e379bfe..7e45c36 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.11.0" +__version__ = "0.12.0-dev" From 4e1f874f2c3b132952301e470e0641c6a1166768 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 26 Jun 2016 13:39:42 -0700 Subject: [PATCH 046/135] Fix #174 --- iptc/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/util.py b/iptc/util.py index a587e7e..7bb97fb 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -75,7 +75,7 @@ def _do_find_library(name): def _find_library(*names): - if version_info > (3, ): + if version_info >= (3, 3): ext = get_config_var("EXT_SUFFIX") else: ext = get_config_var('SO') From e8999bc14123c331c821660ed31c383b9c29e6bb Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Fri, 11 Nov 2016 15:01:18 -0500 Subject: [PATCH 047/135] Update debian files and .gitignore * Sync debian package version to upstream version * Add debhelper-generated files to .gitignore --- .gitignore | 18 ++++++++++++++++++ debian/changelog | 6 ++++++ debian/control | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index eeb8a34..30251e5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,21 @@ /Vagrantfile /.vagrant /tags +*/__pycache__/ +*.pyc +/build/ +/.pybuild/ +*.egg-info + +/debian/python-iptables/ +/debian/python3-iptables/ +/debian/python-iptables-dbg/ +/debian/python3-iptables-dbg/ +/debian/python-iptables-doc/ +/debian/files +/debian/outfile +/debian/*.debhelper +/debian/*.log +/debian/*-stamp +/debian/*.substvars + diff --git a/debian/changelog b/debian/changelog index 0d7c90f..a5ae47d 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +python-iptables (0.12.0-1) xenial; urgency=low + + * update debian changelog + + -- Dan Fuhry Fri, 11 Nov 2016 14:03:32 -0500 + python-iptables (0.5-git-20140925) precise; urgency=low * update debian/ diff --git a/debian/control b/debian/control index 468f1a5..2f82047 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: python-iptables Section: net Priority: extra Maintainer: Juliano Martinez -Build-depends: python-all-dev (>= 2.7), python-all-dbg (>= 2.7), python3-all-dev (>= 3.2), python3-all-dbg, debhelper (>= 7), python-support (>= 0.90) +Build-depends: python-all-dev (>= 2.7), python-all-dbg (>= 2.7), python3-all-dev (>= 3.2), python3-all-dbg, debhelper (>= 7) X-Python-Version: >= 2.7 X-Python3-Version: >= 3.2 Standards-Version: 3.8.4 From eb186ddfb43b5b54c2a43731427edb6a26a52c0c Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Thu, 15 Dec 2016 17:08:20 +0700 Subject: [PATCH 048/135] Initial pepoluan Commit Modify .hgignore to exclude PyCharm working dirs. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index eeb8a34..739a2eb 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ /Vagrantfile /.vagrant /tags + +# Added exclusion for PyCharm files +.idea/* From 2d9eb2b7fad7a1b57287972eb83f765c86b766f4 Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Thu, 15 Dec 2016 17:18:19 +0700 Subject: [PATCH 049/135] Implement -g Support In general, changes made are: * Change constructor of Target to allow 'goto' option * Change create_target method of Rule, same purpose * Change how IPT_F_FRAG gets set in rule.entry.ip --- iptc/ip4tc.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 3c6d42a..35ae04c 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -662,7 +662,7 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string i.e. "" should be used. """ - def __init__(self, rule, name=None, target=None, revision=None): + def __init__(self, rule, name=None, target=None, revision=None, goto=None): """ *rule* is the Rule object this match belongs to; it can be changed later via *set_rule()*. *name* is the name of the iptables target @@ -672,6 +672,7 @@ def __init__(self, rule, name=None, target=None, revision=None): should be used; different revisions use different structures in C and they usually only work with certain kernel versions. Python-iptables by default will use the latest revision available. + If goto is True, then it converts '-j' to '-g'. """ if name is None and target is None: raise ValueError("can't create target based on nothing") @@ -682,6 +683,17 @@ def __init__(self, rule, name=None, target=None, revision=None): self._orig_parse = None self._orig_options = None + if target is not None or goto is None: + # We are 'decoding' existing Target + self._goto = bool(rule.entry.ip.flags & ipt_ip.IPT_F_GOTO) + if goto is not None: + assert isinstance(goto, bool) + self._goto = goto + if goto: + rule.entry.ip.flags |= ipt_ip.IPT_F_GOTO + else: + rule.entry.ip.flags &= ~ipt_ip.IPT_F_GOTO + self._xt = xtables(rule.nfproto) module = (self._is_standard_target() and @@ -831,6 +843,10 @@ def _get_target(self): target = property(_get_target) """This is the C structure used by the extension.""" + def _get_goto(self): + return self._goto + goto = property(_get_goto) + class Policy(object): """ @@ -960,11 +976,11 @@ def create_match(self, name, revision=None): self.add_match(match) return match - def create_target(self, name, revision=None): + def create_target(self, name, revision=None, goto=False): """Create a new *target*, and set it as this rule's target. *name* is the name of the target extension, *revision* is the revision to - use.""" - target = Target(self, name=name, revision=revision) + use. *goto* determines if target uses '-j' (default) or '-g'.""" + target = Target(self, name=name, revision=revision, goto=False) self.target = target return target @@ -1207,7 +1223,10 @@ def get_fragment(self): def set_fragment(self, frag): self.entry.ip.invflags &= ~ipt_ip.IPT_INV_FRAG & ipt_ip.IPT_INV_MASK - self.entry.ip.flags = int(bool(frag)) + if frag: + self.entry.ip.flags |= ipt_ip.IPT_F_FRAG + else: + self.entry.ip.flags &= ~ipt_ip.IPT_F_FRAG fragment = property(get_fragment, set_fragment) """This means that the rule refers to the second and further fragments of From c9da2e93a3a8503fb7aa7a01854cd7d65f341d8d Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Thu, 15 Dec 2016 19:18:38 +0700 Subject: [PATCH 050/135] Bugfix #1 in this branch So create_target()'s' goto parameter did not get passed to Target()... fixed. --- iptc/ip4tc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 35ae04c..f43df7d 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -980,7 +980,7 @@ def create_target(self, name, revision=None, goto=False): """Create a new *target*, and set it as this rule's target. *name* is the name of the target extension, *revision* is the revision to use. *goto* determines if target uses '-j' (default) or '-g'.""" - target = Target(self, name=name, revision=revision, goto=False) + target = Target(self, name=name, revision=revision, goto=goto) self.target = target return target From d3df22b2520fc68c4eaa3852b571704d2d9787eb Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Fri, 16 Dec 2016 01:25:59 +0700 Subject: [PATCH 051/135] Make Compatible with ip6tc Now properly use get_ip() and introspection to ensure compatibility with ip6tc, and future. (IPv8, anyone?) --- iptc/ip4tc.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index f43df7d..19599a9 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -683,16 +683,27 @@ def __init__(self, rule, name=None, target=None, revision=None, goto=None): self._orig_parse = None self._orig_options = None + # NOTE: + # get_ip() returns the 'ip' structure that contains (1)the 'flags' field, and + # (2)the value for the GOTO flag. + # We *must* use get_ip() because the actual name of the field containing the + # structure apparently differs between implementation + ipstruct = rule.get_ip() + f_goto_attrs = [a for a in dir(ipstruct) if a.endswith('_F_GOTO')] + if len(f_goto_attrs) == 0: + raise RuntimeError('What kind of struct is this? It does not have "*_F_GOTO" constant!') + _F_GOTO = getattr(ipstruct, f_goto_attrs[0]) + if target is not None or goto is None: # We are 'decoding' existing Target - self._goto = bool(rule.entry.ip.flags & ipt_ip.IPT_F_GOTO) + self._goto = bool(ipstruct.flags & _F_GOTO) if goto is not None: assert isinstance(goto, bool) self._goto = goto if goto: - rule.entry.ip.flags |= ipt_ip.IPT_F_GOTO + ipstruct.flags |= _F_GOTO else: - rule.entry.ip.flags &= ~ipt_ip.IPT_F_GOTO + ipstruct.flags &= ~_F_GOTO self._xt = xtables(rule.nfproto) From 43db2ed2bef1c04c8b90786e778c57ea70b25de4 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Date: Tue, 3 Jan 2017 18:12:56 +0200 Subject: [PATCH 052/135] Add sctp protocol support. Update tests to include sctp literal protocol and numeric protocol dccp (33) --- iptc/ip4tc.py | 5 +++++ tests/test_iptc.py | 18 +++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 19599a9..3a5791a 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -20,6 +20,10 @@ except: pass +# Add IPPROTO_SCTP to socket module if not available +if not hasattr(socket, 'IPPROTO_SCTP'): + setattr(socket, 'IPPROTO_SCTP', 132) + _IFNAMSIZ = 16 _libc = ct.CDLL("libc.so.6") @@ -932,6 +936,7 @@ class Rule(object): socket.IPPROTO_RAW: "raw", socket.IPPROTO_ROUTING: "routing", socket.IPPROTO_RSVP: "rsvp", + socket.IPPROTO_SCTP: "sctp", socket.IPPROTO_TCP: "tcp", socket.IPPROTO_TP: "tp", socket.IPPROTO_UDP: "udp", diff --git a/tests/test_iptc.py b/tests/test_iptc.py index 6317d20..a0b6ef9 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -432,7 +432,7 @@ def test_rule_interface(self): def test_rule_protocol(self): rule = iptc.Rule6() for proto in ["tcp", "udp", "icmp", "AH", "ESP", "!TCP", "!UDP", - "!ICMP", "!ah", "!esp"]: + "!ICMP", "!ah", "!esp", "sctp", "!SCTP"]: rule.protocol = proto self.assertEquals(proto.lower(), rule.protocol) for proto in ["", "asdf", "!"]: @@ -447,10 +447,10 @@ def test_rule_protocol(self): def test_rule_protocol_numeric(self): rule = iptc.Rule6() - rule.protocol = 132 - self.assertEquals(rule.protocol, '132') - rule.protocol = '!132' - self.assertEquals(rule.protocol, '!132') + rule.protocol = 33 + self.assertEquals(rule.protocol, '33') + rule.protocol = '!33' + self.assertEquals(rule.protocol, '!33') def test_rule_compare(self): r1 = iptc.Rule6() @@ -677,10 +677,10 @@ def test_rule_protocol(self): def test_rule_protocol_numeric(self): rule = iptc.Rule() - rule.protocol = 132 - self.assertEquals(rule.protocol, '132') - rule.protocol = '!132' - self.assertEquals(rule.protocol, '!132') + rule.protocol = 33 + self.assertEquals(rule.protocol, '33') + rule.protocol = '!33' + self.assertEquals(rule.protocol, '!33') def test_rule_compare(self): r1 = iptc.Rule() From bd0928ea3f0219641c1cf879d07ea3bc4bd249ed Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Mon, 27 Feb 2017 16:11:44 -0500 Subject: [PATCH 053/135] Remove package version from Debian version number --- debian/changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index a5ae47d..e8bab22 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -python-iptables (0.12.0-1) xenial; urgency=low +python-iptables (0.12.0) xenial; urgency=low * update debian changelog From e947af15f0cfe3a6d5d1100fdd3c802144a2a1bd Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Mon, 27 Feb 2017 17:25:22 -0500 Subject: [PATCH 054/135] Add /usr/lib/python3/dist-packages to ld search path Fixes module import on older Ubuntu releases --- debian/python3-iptables.postinst | 3 +++ debian/rules | 7 +++++++ 2 files changed, 10 insertions(+) create mode 100644 debian/python3-iptables.postinst diff --git a/debian/python3-iptables.postinst b/debian/python3-iptables.postinst new file mode 100644 index 0000000..7b46270 --- /dev/null +++ b/debian/python3-iptables.postinst @@ -0,0 +1,3 @@ +#!/bin/sh + +/sbin/ldconfig diff --git a/debian/rules b/debian/rules index b8ad1a9..4fd181a 100755 --- a/debian/rules +++ b/debian/rules @@ -21,6 +21,13 @@ override_dh_auto_install: $$py -B setup.py install --root debian/python3-iptables --install-layout deb; \ $$py-dbg -B setup.py install --root debian/python3-iptables-dbg --install-layout deb; \ done + + # On Ubuntu 14.04 and possibly other versions, /usr/lib/python3/dist-packages is not + # included in the system library search path, so we add it here. + set -e; \ + mkdir -p $(CURDIR)/debian/python3-iptables/etc/ld.so.conf.d; \ + echo "/usr/lib/python3/dist-packages" > $(CURDIR)/debian/python3-iptables/etc/ld.so.conf.d/python3-dist-packages.conf + set -e; \ for py in $(PY2VERS); do \ $$py -B setup.py install --root debian/python-iptables --install-layout deb; \ From 09bf65deff578d6e204adfb4d5a4a2e097c46123 Mon Sep 17 00:00:00 2001 From: Santosh Ananthakrishnan Date: Thu, 13 Apr 2017 02:52:51 -0700 Subject: [PATCH 055/135] Fix mask computation for v6 IPs --- iptc/ip6tc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index 1611525..84deeda 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -273,7 +273,7 @@ def _create_mask(self, plen): if plen >= 8: mask.append(0xff) elif plen > 0: - mask.append(2 ** plen - 1) + mask.append(0xff>>(8-plen)<<(8-plen)) else: mask.append(0x00) plen -= 8 From 599d8b6c38ab9a975a9823e7cb594a9b350796af Mon Sep 17 00:00:00 2001 From: Santosh Ananthakrishnan Date: Thu, 13 Apr 2017 18:42:42 -0700 Subject: [PATCH 056/135] Add a test for create_match --- tests/test_iptc.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_iptc.py b/tests/test_iptc.py index a0b6ef9..a645304 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -374,6 +374,21 @@ def tearDown(self): self.chain.flush() self.chain.delete() + def test_create_mask(self): + rule = iptc.Rule6() + + # Mask /10 should return \xff\xc0\x00... + mask = rule._create_mask(10) + self.assertEquals(mask[0], 0xff) + self.assertEquals(mask[1], 0xc0) + self.assertEquals(mask[2:], [0x00]*14) + + # Mask /27 should return \xff\xff\xff\xe0... + mask = rule._create_mask(27) + self.assertEquals(mask[:3], [0xff, 0xff, 0xff]) + self.assertEquals(mask[3], 0xe0) + self.assertEquals(mask[4:], [0x00]*12) + def test_rule_address(self): # valid addresses rule = iptc.Rule6() From 0bc32ec166986e47c1900cf3b1b678bd7c9fffba Mon Sep 17 00:00:00 2001 From: James Oakley Date: Tue, 25 Apr 2017 07:27:04 -0700 Subject: [PATCH 057/135] Add support for xtables v12 --- iptc/xtables.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 2cfccbb..2688594 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -411,6 +411,7 @@ class _xtables_match_v10(ct.Structure): _xtables_match_v11 = _xtables_match_v10 +_xtables_match_v12 = _xtables_match_v10 class xtables_match(ct.Union): @@ -424,7 +425,8 @@ class xtables_match(ct.Union): # Apparently v8 was skipped ("v9", _xtables_match_v9), ("v10", _xtables_match_v10), - ("v11", _xtables_match_v11)] + ("v11", _xtables_match_v11), + ("v11", _xtables_match_v12)] class _xtables_target_v1(ct.Structure): @@ -667,6 +669,60 @@ class _xtables_target_v10(ct.Structure): _xtables_target_v11 = _xtables_target_v10 +class _xtables_target_v12(ct.Structure): + _fields_ = [("version", ct.c_char_p), + ("next", ct.c_void_p), + ("name", ct.c_char_p), + ("real_name", ct.c_char_p), + ("revision", ct.c_uint8), + ("ext_flags", ct.c_uint8), + ("family", ct.c_uint16), + ("size", ct.c_size_t), + ("userspacesize", ct.c_size_t), + ("help", ct.CFUNCTYPE(None)), + ("init", ct.CFUNCTYPE(None, ct.POINTER(xt_entry_target))), + # fourth parameter entry is struct ipt_entry for example + # int (*parse)(int c, char **argv, int invert, + # unsigned int *flags, const void *entry, + # struct xt_entry_target **target) + ("parse", ct.CFUNCTYPE(ct.c_int, + ct.POINTER(ct.c_char_p), ct.c_int, + ct.POINTER(ct.c_uint), ct.c_void_p, + ct.POINTER(ct.POINTER( + xt_entry_target)))), + ("final_check", ct.CFUNCTYPE(None, ct.c_uint)), + # prints out the target iff non-NULL: put space at end + # first parameter ip is struct ipt_ip * for example + ("print", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_target), ct.c_int)), + # saves the target info in parsable form to stdout. + # first parameter ip is struct ipt_ip * for example + ("save", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_target))), + # Print target name or alias + ("alias", ct.CFUNCTYPE(ct.c_char_p, + ct.POINTER(xt_entry_target))), + # pointer to list of extra command-line options + ("extra_opts", ct.POINTER(option)), + + # introduced with the new iptables API + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(xt_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(xt_fcheck_call))), + ("x6_options", ct.POINTER(xt_option_entry)), + + ('xt_xlate', ct.c_int), + + # size of per-extension instance extra "global" scratch space + ("udata_size", ct.c_size_t), + + # ignore these men behind the curtain: + ("udata", ct.c_void_p), + ("option_offset", ct.c_uint), + ("t", ct.POINTER(xt_entry_target)), + ("tflags", ct.c_uint), + ("used", ct.c_uint), + ("loaded", ct.c_uint)] + class xtables_target(ct.Union): _fields_ = [("v1", _xtables_target_v1), @@ -679,8 +735,8 @@ class xtables_target(ct.Union): # Apparently v8 was skipped ("v9", _xtables_target_v9), ("v10", _xtables_target_v10), - ("v11", _xtables_target_v11)] - + ("v11", _xtables_target_v11), + ("v12", _xtables_target_v12)] class XTablesError(Exception): """Raised when an xtables call fails for some reason.""" From b367788dcfacf50a9479d56803e0e9b00a4db02b Mon Sep 17 00:00:00 2001 From: James Oakley Date: Thu, 27 Apr 2017 14:19:37 -0700 Subject: [PATCH 058/135] Fix incorrect version for v12 match --- iptc/xtables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 2688594..245a348 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -426,7 +426,7 @@ class xtables_match(ct.Union): ("v9", _xtables_match_v9), ("v10", _xtables_match_v10), ("v11", _xtables_match_v11), - ("v11", _xtables_match_v12)] + ("v12", _xtables_match_v12)] class _xtables_target_v1(ct.Structure): From 9a940571c7c6e067077bb1617f57d9a7910f51ef Mon Sep 17 00:00:00 2001 From: adisbladis Date: Thu, 4 May 2017 22:28:02 +0800 Subject: [PATCH 059/135] xtables v12 match structure also has member xt_xlate --- iptc/xtables.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 245a348..0d3b40f 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -411,7 +411,59 @@ class _xtables_match_v10(ct.Structure): _xtables_match_v11 = _xtables_match_v10 -_xtables_match_v12 = _xtables_match_v10 + + +class _xtables_match_v12(ct.Structure): + _fields_ = [("version", ct.c_char_p), + ("next", ct.c_void_p), + ("name", ct.c_char_p), + ("real_name", ct.c_char_p), + ("revision", ct.c_uint8), + ("ext_flags", ct.c_uint8), + ("family", ct.c_uint16), + ("size", ct.c_size_t), + ("userspacesize", ct.c_size_t), + ("help", ct.CFUNCTYPE(None)), + ("init", ct.CFUNCTYPE(None, ct.POINTER(xt_entry_match))), + # fourth parameter entry is struct ipt_entry for example + # int (*parse)(int c, char **argv, int invert, unsigned int + # *flags, const void *entry, struct xt_entry_match **match) + ("parse", ct.CFUNCTYPE(ct.c_int, ct.c_int, + ct.POINTER(ct.c_char_p), ct.c_int, + ct.POINTER(ct.c_uint), ct.c_void_p, + ct.POINTER(ct.POINTER( + xt_entry_match)))), + ("final_check", ct.CFUNCTYPE(None, ct.c_uint)), + # prints out the match iff non-NULL: put space at end + # first parameter ip is struct ipt_ip * for example + ("print", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_match), ct.c_int)), + # saves the match info in parsable form to stdout. + # first parameter ip is struct ipt_ip * for example + ("save", ct.CFUNCTYPE(None, ct.c_void_p, + ct.POINTER(xt_entry_match))), + # Print match name or alias + ("alias", ct.CFUNCTYPE(ct.c_char_p, + ct.POINTER(xt_entry_match))), + # pointer to list of extra command-line options + ("extra_opts", ct.POINTER(option)), + + # introduced with the new iptables API + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(xt_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(xt_fcheck_call))), + ("x6_options", ct.POINTER(xt_option_entry)), + + ('xt_xlate', ct.c_int), + + # size of per-extension instance extra "global" scratch space + ("udata_size", ct.c_size_t), + + # ignore these men behind the curtain: + ("udata", ct.c_void_p), + ("option_offset", ct.c_uint), + ("m", ct.POINTER(xt_entry_match)), + ("mflags", ct.c_uint), + ("loaded", ct.c_uint)] class xtables_match(ct.Union): From 81de8a8a65590b7e2cc27ecb4a20ce1ed92584f1 Mon Sep 17 00:00:00 2001 From: Pandu POLUAN Date: Fri, 2 Jun 2017 09:48:27 +0700 Subject: [PATCH 060/135] Known Issues Update README.md with Known Issues --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) mode change 100644 => 100755 README.md diff --git a/README.md b/README.md old mode 100644 new mode 100755 index df46172..0e43e14 --- a/README.md +++ b/README.md @@ -537,3 +537,18 @@ or more rules, than commit it: The drawback is that Table is a singleton, and if you disable autocommit, it will be disabled for all instances of that Table. + + +Known Issues +============ + +These issues are mainly caused by complex interaction with upstream's +Netfilter implementation, and will require quite significant effort to +fix. Workarounds are available. + +* [Issue #201](https://github.com/ldx/python-iptables/issues/201) + -- The `hashlimit` match requires explicitly setting `hashlimit_htable_expire` + +* [Issue #204](https://github.com/ldx/python-iptables/issues/204) + -- The `NOTRACK` target is problematic; use `CT --notrack` instead + From 7ec8b6ea1cebc67a9f0f193149dd098e4ffe0671 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 5 Jun 2017 19:50:34 -0700 Subject: [PATCH 061/135] Release 0.12.0 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 7e45c36..ae0d7fb 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.12.0-dev" +__version__ = "0.12.0" From 60a64b97dd96496189564cef6a734d60055a2656 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 5 Jun 2017 19:56:34 -0700 Subject: [PATCH 062/135] Start 0.13.0-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index ae0d7fb..7f73838 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.12.0" +__version__ = "0.13.0-dev" From 75f8202185306e7eb21b5bd97a8390d7f47e5efb Mon Sep 17 00:00:00 2001 From: Dmitriusan Date: Sun, 11 Jun 2017 12:45:15 +0300 Subject: [PATCH 063/135] Remove useless parentheses from error messages --- iptc/ip4tc.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 3a5791a..a333cd6 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1714,7 +1714,7 @@ def set_policy(self, chain, policy, counters=None): rv = self._iptc.iptc_set_policy(chain.encode(), policy.encode(), cntrs, self._handle) if rv != 1: - raise IPTCError("can't set policy %s on chain %s: %s)" % + raise IPTCError("can't set policy %s on chain %s: %s" % (policy, chain, self.strerror())) @autocommit @@ -1739,7 +1739,7 @@ def append_entry(self, chain, entry): ct.cast(entry, ct.c_void_p), self._handle) if rv != 1: - raise IPTCError("can't append entry to chain %s: %s)" % + raise IPTCError("can't append entry to chain %s: %s" % (chain, self.strerror())) @autocommit @@ -1749,7 +1749,7 @@ def insert_entry(self, chain, entry, position): ct.cast(entry, ct.c_void_p), position, self._handle) if rv != 1: - raise IPTCError("can't insert entry into chain %s: %s)" % + raise IPTCError("can't insert entry into chain %s: %s" % (chain, self.strerror())) @autocommit @@ -1759,7 +1759,7 @@ def replace_entry(self, chain, entry, position): ct.cast(entry, ct.c_void_p), position, self._handle) if rv != 1: - raise IPTCError("can't replace entry in chain %s: %s)" % + raise IPTCError("can't replace entry in chain %s: %s" % (chain, self.strerror())) @autocommit @@ -1769,7 +1769,7 @@ def delete_entry(self, chain, entry, mask): ct.cast(entry, ct.c_void_p), mask, self._handle) if rv != 1: - raise IPTCError("can't delete entry from chain %s: %s)" % + raise IPTCError("can't delete entry from chain %s: %s" % (chain, self.strerror())) def first_rule(self, chain): From b6a1c4c91a48dc527ce8d003c11e536fd3683802 Mon Sep 17 00:00:00 2001 From: Pajaro_xdd Date: Thu, 31 Aug 2017 01:43:47 +0200 Subject: [PATCH 064/135] modified: iptc/xtables.py Add new xtdir to fix error on Debian --- iptc/xtables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 0d3b40f..dd8033c 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -804,7 +804,7 @@ class XTablesError(Exception): import os.path for xtdir in ["/lib/xtables", "/lib64/xtables", "/usr/lib/xtables", "/usr/lib/iptables", "/usr/lib64/xtables", - "/usr/lib64/iptables", "/usr/local/lib/xtables"]: + "/usr/lib64/iptables", "/usr/local/lib/xtables", "/usr/lib/x86_64-linux-gnu/xtables"]: if os.path.isdir(xtdir): _xtables_libdir = xtdir break From 714e455795406c3c3ac8a93f7a74c172df2fb3a6 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 1 Sep 2017 21:03:54 +0100 Subject: [PATCH 065/135] correct spelling mistake --- iptc/ip4tc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index a333cd6..d72d1b7 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -102,7 +102,7 @@ class ipt_entry(ct.Structure): class IPTCError(Exception): """This exception is raised when a low-level libiptc error occurs. - It contains a short description about the error that occured while + It contains a short description about the error that occurred while executing an iptables operation. """ From c0d2400e25baa5c3c7a6b67e8483e821f84dbcda Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Fri, 1 Sep 2017 13:34:33 -0700 Subject: [PATCH 066/135] The source for docs is the RST files in doc/. --- doc/examples.rst | 30 ++++++++++++++++++++---------- doc/intro.rst | 10 +++++++++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/doc/examples.rst b/doc/examples.rst index 5efbf42..b17c005 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -153,19 +153,19 @@ in/out inteface etc is. To print out all rules in the FILTER table:: >>> print "=======================" As you see in the code snippet above, rules are organized into chains, and -chains are in tables. You have a fixed set of tables; for IPv4:: +chains are in tables. You have a fixed set of tables; for IPv4: -* FILTER, -* NAT, -* MANGLE and -* RAW. +* ``FILTER``, +* ``NAT``, +* ``MANGLE`` and +* ``RAW``. -For IPv6 the tables are:: +For IPv6 the tables are: -* FILTER, -* MANGLE, -* RAW and -* SECURITY. +* ``FILTER``, +* ``MANGLE``, +* ``RAW`` and +* ``SECURITY``. To access a table:: @@ -409,3 +409,13 @@ commit it:: The drawback is that `Table` is a singleton, and if you disable autocommit, it will be disabled for all instances of that `Table`. + +Known Issues +============ + +These issues are mainly caused by complex interaction with upstream's +Netfilter implementation, and will require quite significant effort to +fix. Workarounds are available. + +- The ``hashlimit`` match requires explicitly setting ``hashlimit_htable_expire``. See `Issue #201 `_. +- The ``NOTRACK`` target is problematic; use ``CT --notrack`` instead. See `Issue #204 `_. diff --git a/doc/intro.rst b/doc/intro.rst index eab00f2..97ec5a0 100644 --- a/doc/intro.rst +++ b/doc/intro.rst @@ -44,6 +44,14 @@ If you are looking for ``ebtables`` python bindings, check out :target: https://travis-ci.org/ldx/python-iptables :alt: Build Status +.. image:: https://coveralls.io/repos/ldx/python-iptables/badge.svg?branch=codecoverage + :target: https://coveralls.io/r/ldx/python-iptables?branch=codecoverage + :alt: Coverage Status + +.. image:: https://landscape.io/github/ldx/python-iptables/codecoverage/landscape.svg + :target: https://landscape.io/github/ldx/python-iptables/codecoverage + :alt: Code Health + .. image:: https://pypip.in/d/python-iptables/badge.png :target: https://pypi.python.org/pypi/python-iptables :alt: Number of Downloads @@ -85,7 +93,7 @@ installs into ``/usr/local/lib``. Now you can run the tests:: - % sudo PATH=$PATH ./test.py + % sudo PATH=$PATH python setup.py test WARNING: this test will manipulate iptables rules. Don't do this on a production machine. Would you like to continue? y/n y From a0c9efe598b0411cda5795afdd358aa1bab13359 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Fri, 1 Sep 2017 13:35:24 -0700 Subject: [PATCH 067/135] Rebuild README.md via pandoc -o ../README.md -f rst -t markdown intro.rst examples.rst. --- README.md | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0e43e14..dbc6cd6 100755 --- a/README.md +++ b/README.md @@ -36,7 +36,11 @@ If you are looking for `ebtables` python bindings, check out [![Latest Release](https://pypip.in/v/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) -[![Build Status](https://travis-ci.org/ldx/python-iptables.png?branch=master)](https://travis-ci.org/ldx/python-iptables) [![Coverage Status](https://coveralls.io/repos/ldx/python-iptables/badge.svg?branch=codecoverage)](https://coveralls.io/r/ldx/python-iptables?branch=codecoverage) [![Code Health](https://landscape.io/github/ldx/python-iptables/codecoverage/landscape.svg)](https://landscape.io/github/ldx/python-iptables/codecoverage) +[![Build Status](https://travis-ci.org/ldx/python-iptables.png?branch=master)](https://travis-ci.org/ldx/python-iptables) + +[![Coverage Status](https://coveralls.io/repos/ldx/python-iptables/badge.svg?branch=codecoverage)](https://coveralls.io/r/ldx/python-iptables?branch=codecoverage) + +[![Code Health](https://landscape.io/github/ldx/python-iptables/codecoverage/landscape.svg)](https://landscape.io/github/ldx/python-iptables/codecoverage) [![Number of Downloads](https://pypip.in/d/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) @@ -77,6 +81,10 @@ installs into `/usr/local/lib`. Now you can run the tests: % sudo PATH=$PATH python setup.py test + WARNING: this test will manipulate iptables rules. + Don't do this on a production machine. + Would you like to continue? y/n y + [...] The `PATH=$PATH` part is necessary after `sudo` if you have installed into a `virtualenv`, since `sudo` will reset your environment to a @@ -279,17 +287,17 @@ in/out inteface etc is. To print out all rules in the FILTER table: As you see in the code snippet above, rules are organized into chains, and chains are in tables. You have a fixed set of tables; for IPv4: - * FILTER, - * NAT, - * MANGLE and - * RAW. +- `FILTER`, +- `NAT`, +- `MANGLE` and +- `RAW`. For IPv6 the tables are: - * FILTER, - * MANGLE, - * RAW and - * SECURITY. +- `FILTER`, +- `MANGLE`, +- `RAW` and +- `SECURITY`. To access a table: @@ -538,7 +546,6 @@ or more rules, than commit it: The drawback is that Table is a singleton, and if you disable autocommit, it will be disabled for all instances of that Table. - Known Issues ============ @@ -546,9 +553,9 @@ These issues are mainly caused by complex interaction with upstream's Netfilter implementation, and will require quite significant effort to fix. Workarounds are available. -* [Issue #201](https://github.com/ldx/python-iptables/issues/201) - -- The `hashlimit` match requires explicitly setting `hashlimit_htable_expire` - -* [Issue #204](https://github.com/ldx/python-iptables/issues/204) - -- The `NOTRACK` target is problematic; use `CT --notrack` instead +- The `hashlimit` match requires explicitly setting + `hashlimit_htable_expire`. See [Issue + \#201](https://github.com/ldx/python-iptables/issues/201). +- The `NOTRACK` target is problematic; use `CT --notrack` instead. See + [Issue \#204](https://github.com/ldx/python-iptables/issues/204). From cd14daa58290ccbe8639f18a61480f71078fb260 Mon Sep 17 00:00:00 2001 From: Pajaro_xdd Date: Thu, 7 Sep 2017 16:35:17 +0200 Subject: [PATCH 068/135] modified: iptc/xtables.py Strip lines to <80 chars long --- iptc/xtables.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index dd8033c..03644cc 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -804,7 +804,8 @@ class XTablesError(Exception): import os.path for xtdir in ["/lib/xtables", "/lib64/xtables", "/usr/lib/xtables", "/usr/lib/iptables", "/usr/lib64/xtables", - "/usr/lib64/iptables", "/usr/local/lib/xtables", "/usr/lib/x86_64-linux-gnu/xtables"]: + "/usr/lib64/iptables", "/usr/local/lib/xtables", + "/usr/lib/x86_64-linux-gnu/xtables"]: if os.path.isdir(xtdir): _xtables_libdir = xtdir break From b4a53cbe1c788ed7b6a2f44ed7f2dda6b8e25f53 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Date: Tue, 24 Oct 2017 12:17:38 +0300 Subject: [PATCH 069/135] Added missing table definitions to Table and Table6 classes --- iptc/ip4tc.py | 4 +++- iptc/ip6tc.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index d72d1b7..8afdbb9 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1552,7 +1552,9 @@ class Table(object): """This is the constant for the raw table.""" NAT = "nat" """This is the constant for the nat table.""" - ALL = ["filter", "mangle", "raw", "nat"] + SECURITY = "security" + """This is the constant for the security table.""" + ALL = ["filter", "mangle", "raw", "nat", "security"] """This is the constant for all tables.""" _cache = dict() diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index 84deeda..ed10e17 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -571,9 +571,11 @@ class Table6(Table): """This is the constant for the mangle table.""" RAW = "raw" """This is the constant for the raw table.""" + NAT = "nat" + """This is the constant for the nat table.""" SECURITY = "security" """This is the constant for the security table.""" - ALL = ["filter", "mangle", "raw", "security"] + ALL = ["filter", "mangle", "raw", "nat", "security"] """This is the constant for all tables.""" _cache = dict() From acb035200c14a61f7df7b9f5815f11957a5ca768 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Date: Mon, 13 Nov 2017 03:09:10 +0200 Subject: [PATCH 070/135] Added dictionary functionality to Rule(6) class --- iptc/ip4tc.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 8afdbb9..5309cee 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -10,7 +10,7 @@ import weakref from .util import find_library, load_kernel -from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables, +from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, NFPROTO_IPV6, XTablesError, xtables, xt_align, xt_counters, xt_entry_target, xt_entry_match) __all__ = ["Table", "Chain", "Rule", "Match", "Target", "Policy", "IPTCError"] @@ -1389,6 +1389,119 @@ def _get_mask(self): mask = property(_get_mask) """This is the raw mask buffer as iptables uses it when removing rules.""" + @classmethod + def from_dict(cls, rule_d): + """Generate a Rule(6) object from the input dictionary.""" + # Sanity check + assert(isinstance(rule_d, dict)) + # Basic rule attributes + rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') + iptc_rule = cls() + # Avoid issues with matches that require basic parameters to be configured first + for name in rule_attr: + if name in rule_d: + _iptc_setrule(iptc_rule, name, rule_d[name]) + for name, value in rule_d.items(): + try: + if name in rule_attr: + #_iptc_setrule(iptc_rule, name, value) + continue + elif name == 'target': + _iptc_settarget(iptc_rule, value) + else: + _iptc_setmatch(iptc_rule, name, value) + except Exception as e: + #print('Ignoring unsupported field <{}:{}>'.format(name, value)) + continue + return iptc_rule + + def to_dict(self): + """Generate a dictionary representation of the Rule(6) object.""" + d = {} + if self.nfproto==NFPROTO_IPV4 and self.src != '0.0.0.0/0.0.0.0': + d['src'] = self.src + elif self.nfproto==NFPROTO_IPV6 and self.src != '::/0': + d['src'] = self.src + if self.nfproto==NFPROTO_IPV4 and self.dst != '0.0.0.0/0.0.0.0': + d['dst'] = self.dst + elif self.nfproto==NFPROTO_IPV6 and self.dst != '::/0': + d['dst'] = self.dst + if self.protocol != 'ip': + d['protocol'] = self.protocol + if self.in_interface is not None: + d['in-interface'] = self.in_interface + if self.out_interface is not None: + d['out-interface'] = self.out_interface + if self.nfproto==NFPROTO_IPV4 and self.fragment: + d['fragment'] = self.fragment + for m in self.matches: + if m.name not in d: + d[m.name] = m.get_all_parameters() + elif isinstance(d[m.name], list): + d[m.name].append(m.get_all_parameters()) + else: + d[m.name] = [d[m.name], m.get_all_parameters()] + if self.target and self.target.name and len(self.target.get_all_parameters()): + name = self.target.name.replace('-', '_') + d['target'] = {name:self.target.get_all_parameters()} + elif self.target and self.target.name: + d['target'] = self.target.name + # Return a filtered dictionary + return _filter_empty_field(d) + +# Helper functions for dictionary operations over Rule(6) objects +def _iptc_setattr(object, name, value): + # Translate attribute name + name = name.replace('-', '_') + setattr(object, name, value) + +def _iptc_setattr_d(object, value_d): + for name, value in value_d.items(): + _iptc_setattr(object, name, value) + +def _iptc_setrule(iptc_rule, name, value): + _iptc_setattr(iptc_rule, name, value) + +def _iptc_setmatch(iptc_rule, name, value): + # Iterate list/tuple recursively + if isinstance(value, list) or isinstance(value, tuple): + for inner_value in value: + _iptc_setmatch(iptc_rule, name, inner_value) + # Assign dictionary value + elif isinstance(value, dict): + iptc_match = iptc_rule.create_match(name) + _iptc_setattr_d(iptc_match, value) + # Assign value directly + else: + iptc_match = iptc_rule.create_match(name) + _iptc_setattr(iptc_match, name, value) + +def _iptc_settarget(iptc_rule, value): + # Target is dictionary - Use only 1 pair key/value + if isinstance(value, dict): + for k, v in value.items(): + iptc_target = iptc_rule.create_target(k) + _iptc_setattr_d(iptc_target, v) + return + # Simple target + else: + iptc_target = iptc_rule.create_target(value) + +def _filter_empty_field(data_d): + """ + Remove empty lists from dictionary values + Before: {'target': {'CHECKSUM': {'checksum-fill': []}}} + After: {'target': {'CHECKSUM': {'checksum-fill': ''}}} + """ + for k, v in data_d.items(): + if isinstance(v, dict): + data_d[k] = _filter_empty_field(v) + elif isinstance(v, list) and len(v) != 0: + v = [_filter_empty_field(_v) if isinstance(_v, dict) else _v for _v in v ] + elif isinstance(v, list) and len(v) == 0: + data_d[k] = '' + return data_d + class Chain(object): """Rules are contained by chains. From 43730cbc138c894ca0d9ce56275fc610e71c75d5 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Wed, 31 Jan 2018 14:06:57 -0800 Subject: [PATCH 071/135] don't call ctypes.util.find_library on non-sonames If IPTABLES_LIBDIR is given, we will pass potential paths to _do_find_library. In this case, calling ctypes.util.find_library is not helpful, and we should simply try to load the library directly. --- iptc/util.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/iptc/util.py b/iptc/util.py index 7bb97fb..ae5fb9b 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -52,6 +52,11 @@ def load_kernel(name, exc_if_failed=False): def _do_find_library(name): + if '/' in name: + try: + return ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL) + except Exception: + return None p = ctypes.util.find_library(name) if p: lib = ctypes.CDLL(p, mode=ctypes.RTLD_GLOBAL) From d4537ea14b5bb594c2676cfb9bd95529645f7d81 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Wed, 31 Jan 2018 13:53:09 -0800 Subject: [PATCH 072/135] allow explicitly picking the xtables version If I have multiple libxtable v 80ersions, I might want python-iptables to use a version that doesn't come from the libxtables.so symlink. Add a PYTHON_IPTABLES_XTABLES_VERSION that facilitates this. For example, $ IPTABLES_LIBDIR=/usr/lib/x86_64-linux-gnu/ PYTHON_IPTABLES_XTABLES_VERSION=11 somecommand.py uses version 11 of xtables. --- iptc/xtables.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 03644cc..ffc3d41 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -798,10 +798,14 @@ class XTablesError(Exception): _optind = ct.c_long.in_dll(_libc, "optind") _optarg = ct.c_char_p.in_dll(_libc, "optarg") -_lib_xtables, xtables_version = find_library("xtables") +xtables_version = os.getenv("PYTHON_IPTABLES_XTABLES_VERSION") +if xtables_version: + _searchlib = "libxtables.so.%s" % (xtables_version,) +else: + _searchlib = "xtables" +_lib_xtables, xtables_version = find_library(_searchlib) _xtables_libdir = os.getenv("XTABLES_LIBDIR") if _xtables_libdir is None: - import os.path for xtdir in ["/lib/xtables", "/lib64/xtables", "/usr/lib/xtables", "/usr/lib/iptables", "/usr/lib64/xtables", "/usr/lib64/iptables", "/usr/local/lib/xtables", From 0c3c0cb0039ff6881e115fb4af6caacdba5fb0ff Mon Sep 17 00:00:00 2001 From: Michael Overmeyer Date: Mon, 5 Mar 2018 19:26:55 +0000 Subject: [PATCH 073/135] Switched broken pypip.in badges to shields.io --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dbc6cd6..60ffa3c 100755 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ If you are looking for `ebtables` python bindings, check out [![Flattr](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=ldx&url=https%3A%2F%2Fgithub.com%2Fldx%2Fpython-iptables) -[![Latest Release](https://pypip.in/v/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) +[![Latest Release](https://img.shields.io/pypi/v/python-iptables.svg)](https://pypi.python.org/pypi/python-iptables) [![Build Status](https://travis-ci.org/ldx/python-iptables.png?branch=master)](https://travis-ci.org/ldx/python-iptables) @@ -42,9 +42,9 @@ If you are looking for `ebtables` python bindings, check out [![Code Health](https://landscape.io/github/ldx/python-iptables/codecoverage/landscape.svg)](https://landscape.io/github/ldx/python-iptables/codecoverage) -[![Number of Downloads](https://pypip.in/d/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) +[![Number of Downloads](https://img.shields.io/pypi/dm/python-iptables.svg)](https://pypi.python.org/pypi/python-iptables) -[![License](https://pypip.in/license/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) +[![License](https://img.shields.io/pypi/l/python-iptables.svg)](https://pypi.python.org/pypi/python-iptables) Installing via pip ------------------ From 92e08e201731b3ca8cad0ee38d43a4a932857b0f Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 27 Dec 2017 09:16:45 -0800 Subject: [PATCH 074/135] Create separate instances for IPv4 and IPv6 Chains --- iptc/ip4tc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 8afdbb9..29d77ea 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1400,10 +1400,11 @@ class Chain(object): _cache = weakref.WeakValueDictionary() def __new__(cls, table, name): - obj = Chain._cache.get(table.name + "." + name, None) + table_name = type(table).__name__ + "." + table.name + obj = Chain._cache.get(table_name + "." + name, None) if not obj: obj = object.__new__(cls) - Chain._cache[table.name + "." + name] = obj + Chain._cache[table_name + "." + name] = obj return obj def __init__(self, table, name): From 2abdb1d6103cdd4859a8a5a7e60f4d76ab641f95 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Mon, 19 Mar 2018 08:23:54 +0200 Subject: [PATCH 075/135] Improved filtering of empty fields --- iptc/ip4tc.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 5309cee..2b6114f 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1492,12 +1492,16 @@ def _filter_empty_field(data_d): Remove empty lists from dictionary values Before: {'target': {'CHECKSUM': {'checksum-fill': []}}} After: {'target': {'CHECKSUM': {'checksum-fill': ''}}} + Before: {'tcp': {'dport': ['22']}}} + After: {'tcp': {'dport': '22'}}} """ for k, v in data_d.items(): if isinstance(v, dict): data_d[k] = _filter_empty_field(v) elif isinstance(v, list) and len(v) != 0: v = [_filter_empty_field(_v) if isinstance(_v, dict) else _v for _v in v ] + if isinstance(v, list) and len(v) == 1: + data_d[k] = v.pop() elif isinstance(v, list) and len(v) == 0: data_d[k] = '' return data_d From 54aee9a3b1dcefdadccceca721f03801ee2a48b8 Mon Sep 17 00:00:00 2001 From: Vitaly Greck Date: Sun, 1 Apr 2018 14:08:14 -0700 Subject: [PATCH 076/135] Move errors out to the separate module to be able to catch the import level errors Autodetect lib dirs and import xtables --- iptc/__init__.py | 6 +++--- iptc/errors.py | 8 ++++++++ iptc/xtables.py | 28 ++++++++++++++++++---------- 3 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 iptc/errors.py diff --git a/iptc/__init__.py b/iptc/__init__.py index 9818014..191f60b 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -7,9 +7,9 @@ .. moduleauthor:: Vilmos Nebehaj """ -from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target, - Policy, IPTCError) +from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target, Policy, IPTCError) from iptc.ip6tc import is_table6_available, Table6, Rule6 -from iptc.xtables import XTablesError +from iptc.errors import * + __all__ = [] diff --git a/iptc/errors.py b/iptc/errors.py new file mode 100644 index 0000000..fd25d9a --- /dev/null +++ b/iptc/errors.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- + + +class XTablesError(Exception): + """Raised when an xtables call fails for some reason.""" + + +__all__ = ['XTablesError'] diff --git a/iptc/xtables.py b/iptc/xtables.py index ffc3d41..88cdf3a 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -7,6 +7,7 @@ from . import version from .util import find_library +from .errors import * XT_INV_PROTO = 0x40 # invert the sense of PROTO @@ -790,9 +791,6 @@ class xtables_target(ct.Union): ("v11", _xtables_target_v11), ("v12", _xtables_target_v12)] -class XTablesError(Exception): - """Raised when an xtables call fails for some reason.""" - _libc, _ = find_library("c") _optind = ct.c_long.in_dll(_libc, "optind") @@ -806,13 +804,23 @@ class XTablesError(Exception): _lib_xtables, xtables_version = find_library(_searchlib) _xtables_libdir = os.getenv("XTABLES_LIBDIR") if _xtables_libdir is None: - for xtdir in ["/lib/xtables", "/lib64/xtables", "/usr/lib/xtables", - "/usr/lib/iptables", "/usr/lib64/xtables", - "/usr/lib64/iptables", "/usr/local/lib/xtables", - "/usr/lib/x86_64-linux-gnu/xtables"]: - if os.path.isdir(xtdir): - _xtables_libdir = xtdir - break + import re + ldconfig_path_regex = re.compile('^(/.*):$') + import subprocess + ldconfig = subprocess.Popen( + ('ldconfig', '-N', '-v'), + stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True + ) + ldconfig_out, ldconfig_err = ldconfig.communicate() + if ldconfig.returncode != 0: + raise XTablesError("ldconfig failed, please set XTABLES_LIBDIR") + for ldconfig_out_line in ldconfig_out.splitlines(): + ldconfig_path_regex_match = ldconfig_path_regex.match(ldconfig_out_line) + if ldconfig_path_regex_match is not None: + ldconfig_path = ldconfig_path_regex_match.group(1) + if os.path.isdir(ldconfig_path): + _xtables_libdir = ldconfig_path + break if _xtables_libdir is None: raise XTablesError("can't find directory with extensions; " "please set XTABLES_LIBDIR") From b755c0b3e416c6283ddb1998125427614345a850 Mon Sep 17 00:00:00 2001 From: Vitaly Greck Date: Sun, 1 Apr 2018 16:40:33 -0700 Subject: [PATCH 077/135] Change subprocess.DEVNULL to subprocess.PIPE - compatibility issues --- iptc/xtables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 88cdf3a..d8ff59d 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -809,7 +809,7 @@ class xtables_target(ct.Union): import subprocess ldconfig = subprocess.Popen( ('ldconfig', '-N', '-v'), - stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True ) ldconfig_out, ldconfig_err = ldconfig.communicate() if ldconfig.returncode != 0: From 971c44d20949fd17fca0e6f395208fe006e64322 Mon Sep 17 00:00:00 2001 From: Vitaly Greck Date: Sun, 1 Apr 2018 16:45:41 -0700 Subject: [PATCH 078/135] Add 'xtables' to the ldconfig path - sorry, mistake --- iptc/xtables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index d8ff59d..179649d 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -817,7 +817,7 @@ class xtables_target(ct.Union): for ldconfig_out_line in ldconfig_out.splitlines(): ldconfig_path_regex_match = ldconfig_path_regex.match(ldconfig_out_line) if ldconfig_path_regex_match is not None: - ldconfig_path = ldconfig_path_regex_match.group(1) + ldconfig_path = os.path.join(ldconfig_path_regex_match.group(1), 'xtables') if os.path.isdir(ldconfig_path): _xtables_libdir = ldconfig_path break From 2931bab42a6fb97ee9d0b88fef34363c7f8c609e Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 16 Apr 2018 17:19:37 -0700 Subject: [PATCH 079/135] Release 0.13.0 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 7f73838..205cedd 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.13.0-dev" +__version__ = "0.13.0" From e42e691732ec93d1bc05a954012246e78827f8d3 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Mon, 16 Apr 2018 17:21:13 -0700 Subject: [PATCH 080/135] Start 0.14.0-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 205cedd..978a432 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.13.0" +__version__ = "0.14.0-dev" From 0f8f08a4f7bdb645d3601959dad3320386cbaa4f Mon Sep 17 00:00:00 2001 From: Jesus Llorente Date: Sun, 17 Feb 2019 19:58:16 +0100 Subject: [PATCH 081/135] Update doc/examples.rst Added example of use --- doc/examples.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/examples.rst b/doc/examples.rst index b17c005..1a8fc63 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -410,6 +410,21 @@ commit it:: The drawback is that `Table` is a singleton, and if you disable autocommit, it will be disabled for all instances of that `Table`. +Easy rules with dictionaries +---------------------------- +To simplify operations with ``python-iptables`` rules we have included support to define and convert Rules object into python dictionaries. + + >>> import iptc + >>> table = iptc.Table(iptc.Table.FILTER) + >>> chain = iptc.Chain(table, "INPUT") + >>> # Create an iptc.Rule object from dictionary + >>> rule_d = {'comment': {'comment': 'Match tcp.22'}, 'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}} + >>> rule = iptc.Rule.from_dict(rule_d) + >>> # Obtain a dictionary representation from the iptc.Rule + >>> rule.to_dict() + {'tcp': {'dport': '22'}, 'protocol': 'tcp', 'comment': {'comment': 'Match tcp.22'}, 'target': 'ACCEPT'} + + Known Issues ============ From edabe7c7529e684533ca001be17ff649f0cf6d3b Mon Sep 17 00:00:00 2001 From: Jesus Llorente Date: Sun, 17 Feb 2019 19:22:32 +0000 Subject: [PATCH 082/135] Added test cases for to_dict() and from_dict() --- tests/test_iptc.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_iptc.py b/tests/test_iptc.py index a645304..c01abef 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -574,6 +574,23 @@ def test_rule_insert(self): self.failUnless(rule in crules) crules.remove(rule) + def test_rule_to_dict(self): + rule = iptc.Rule6() + rule.protocol = "tcp" + rule.src = "::1" + target = iptc.Target(rule, "ACCEPT") + rule.target = target + rule_d = rule.to_dict() + self.assertEqual(rule_d, {"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}) + + def test_rule_from_dict(self): + rule = iptc.Rule6() + rule.protocol = "tcp" + rule.src = "::1" + target = iptc.Target(rule, "ACCEPT") + rule.target = target + rule2 = iptc.Rule6.from_dict({"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}) + self.assertEqual(rule, rule2) class TestRule(unittest.TestCase): def setUp(self): @@ -915,6 +932,23 @@ def test_rule_delete_nat(self): self.table_nat.commit() self.table_nat.refresh() + def test_rule_to_dict(self): + rule = iptc.Rule() + rule.protocol = "tcp" + rule.src = "127.0.0.1" + target = iptc.Target(rule, "ACCEPT") + rule.target = target + rule_d = rule.to_dict() + self.assertEqual(rule_d, {"protocol": "tcp", "src": "127.0.0.1/255.255.255.255", "target": "ACCEPT"}) + + def test_rule_from_dict(self): + rule = iptc.Rule() + rule.protocol = "tcp" + rule.src = "127.0.0.1" + target = iptc.Target(rule, "ACCEPT") + rule.target = target + rule2 = iptc.Rule.from_dict({"protocol": "tcp", "src": "127.0.0.1/255.255.255.255", "target": "ACCEPT"}) + self.assertEqual(rule, rule2) def suite(): suite_table6 = unittest.TestLoader().loadTestsFromTestCase(TestTable6) From 2584d477d2fa134719cc6ac5d434a75c44bae076 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 21:35:08 +0000 Subject: [PATCH 083/135] Added easy.py module with modified imports --- iptc/__init__.py | 1 + iptc/easy.py | 485 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 486 insertions(+) create mode 100644 iptc/easy.py diff --git a/iptc/__init__.py b/iptc/__init__.py index 191f60b..68c19bc 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -10,6 +10,7 @@ from iptc.ip4tc import (is_table_available, Table, Chain, Rule, Match, Target, Policy, IPTCError) from iptc.ip6tc import is_table6_available, Table6, Rule6 from iptc.errors import * +import iptc.easy __all__ = [] diff --git a/iptc/easy.py b/iptc/easy.py new file mode 100644 index 0000000..bc7021b --- /dev/null +++ b/iptc/easy.py @@ -0,0 +1,485 @@ +# TODO +# - Expose POLICY of a table + +from .ip4tc import Rule, Table, Chain, IPTCError +from .ip6tc import Rule6, Table6 + +BATCH_MODE = False + +def flush_all(ipv6=False): + """ Flush all available tables """ + for table in get_tables(ipv6): + flush_table(table, ipv6) + +def flush_table(table, ipv6=False, silent=False): + """ Flush a table """ + try: + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.flush() + except Exception as e: + if not silent: raise + +def flush_chain(table, chain, ipv6=False, silent=False): + """ Flush a chain in table """ + try: + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_chain.flush() + except Exception as e: + if not silent: raise + +def zero_all(table, ipv6=False): + """ Zero all tables """ + table_cls = Table6 if ipv6 else Table + for table in table_cls.ALL: + zero_table(table, ipv6) + +def zero_table(table, ipv6=False): + """ Zero a table """ + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.zero_entries() + +def zero_chain(table, chain, ipv6=False): + """ Zero a chain in table """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_chain.zero_counters() + +def has_chain(table, chain, ipv6=False): + """ Return True if chain exists in table False otherwise """ + return _iptc_gettable(table, ipv6).is_chain(chain) + +def has_rule(table, chain, rule_d, ipv6=False): + """ Return True if rule exists in chain False otherwise """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + return iptc_rule in iptc_chain.rules + +def add_chain(table, chain, ipv6=False, silent=False): + """ Return True if chain was added successfully to a table, raise Exception otherwise """ + try: + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.create_chain(chain) + return True + except Exception as e: + if not silent: raise + return False + +def add_rule(table, chain, rule_d, position=0, ipv6=False): + """ Add a rule to a chain in a given position. 0=append, 1=first, n=nth position """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + if position == 0: + # Insert rule in last position -> append + iptc_chain.append_rule(iptc_rule) + elif position > 0: + # Insert rule in given position -> adjusted as iptables CLI + iptc_chain.insert_rule(iptc_rule, position - 1) + elif position < 0: + # Insert rule in given position starting from bottom -> not available in iptables CLI + nof_rules = len(iptc_chain.rules) + _position = position + nof_rules + # Insert at the top if the position has looped over + if _position <= 0: + _position = 0 + iptc_chain.insert_rule(iptc_rule, _position) + +def insert_rule(table, chain, rule_d, ipv6=False): + """ Add a rule to a chain in the 1st position """ + add_rule(table, chain, rule_d, position=1, ipv6=ipv6) + +def delete_chain(table, chain, ipv6=False, flush=False, silent=False): + """ Delete a chain """ + try: + if flush: + flush_chain(table, chain, ipv6, silent) + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.delete_chain(chain) + except Exception as e: + if not silent: raise + +def delete_rule(table, chain, rule_d, ipv6=False, silent=False): + """ Delete a rule from a chain """ + try: + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_chain.delete_rule(iptc_rule) + except Exception as e: + if not silent: raise + +def get_tables(ipv6=False): + """ Get all tables """ + iptc_tables = _iptc_gettables(ipv6) + return [t.name for t in iptc_tables] + +def get_chains(table, ipv6=False): + """ Return the existing chains of a table """ + iptc_table = _iptc_gettable(table, ipv6) + return [iptc_chain.name for iptc_chain in iptc_table.chains] + +def get_rule(table, chain, position=0, ipv6=False, silent=False): + """ Get a rule from a chain in a given position. 0=all rules, 1=first, n=nth position """ + try: + if position == 0: + # Return all rules + return dump_chain(table, chain, ipv6) + elif position > 0: + # Return specific rule by position + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = iptc_chain.rules[position - 1] + return _decode_iptc_rule(iptc_rule, ipv6) + elif position < 0: + # Return last rule -> not available in iptables CLI + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = iptc_chain.rules[position] + return _decode_iptc_rule(iptc_rule, ipv6) + except Exception as e: + if not silent: raise + +def replace_rule(table, chain, old_rule_d, new_rule_d, ipv6=False): + """ Replaces an existing rule of a chain """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_old_rule = _encode_iptc_rule(old_rule_d, ipv6) + iptc_new_rule = _encode_iptc_rule(new_rule_d, ipv6) + iptc_chain.replace_rule(iptc_new_rule, iptc_chain.rules.index(iptc_old_rule)) + +def get_rule_statistics(table, chain, rule_d, ipv6=False): + """ Return a tuple with the rule counters (numberOfBytes, numberOfPackets) """ + if not has_rule(table, chain, rule_d, ipv6): + raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d)) + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule_index = iptc_chain.rules.index(iptc_rule) + return iptc_chain.rules[iptc_rule_index].get_counters() + +def get_rule_position(table, chain, rule_d, ipv6=False): + """ Return the position of a rule within a chain """ + if not has_rule(table, chain, rule_d): + raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d)) + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + return iptc_chain.rules.index(iptc_rule) + + +def test_rule(rule_d, ipv6=False): + """ Return True if the rule is a well-formed dictionary, False otherwise """ + try: + _encode_iptc_rule(rule_d, ipv6) + return True + except: + return False + +def test_match(name, value, ipv6=False): + """ Return True if the match is valid, False otherwise """ + try: + iptc_rule = Rule6() if ipv6 else Rule() + _iptc_setmatch(iptc_rule, name, value) + return True + except: + return False + +def test_target(name, value, ipv6=False): + """ Return True if the target is valid, False otherwise """ + try: + iptc_rule = Rule6() if ipv6 else Rule() + _iptc_settarget(iptc_rule, {name:value}) + return True + except: + return False + + +def dump_all(ipv6=False): + """ Return a dictionary representation of all tables """ + d = {} + for table in get_tables(ipv6): + d[table] = dump_table(table, ipv6) + return d + +def dump_table(table, ipv6=False): + """ Return a dictionary representation of a table """ + d = {} + iptc_table = _iptc_gettable(table, ipv6) + for iptc_chain in iptc_table.chains: + d[iptc_chain.name] = dump_chain(iptc_table.name, iptc_chain.name, ipv6) + return d + +def dump_chain(table, chain, ipv6=False): + """ Return a list with the dictionary representation of the rules of a table """ + l = [] + iptc_chain = _iptc_getchain(table, chain, ipv6) + for i, iptc_rule in enumerate(iptc_chain.rules): + l.append(_decode_iptc_rule(iptc_rule, ipv6)) + return l + + +def batch_begin(table = None, ipv6=False): + """ Disable autocommit on a table """ + BATCH_MODE = True + if table: + tables = (table, ) + else: + tables = get_tables(ipv6) + for table in tables: + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.autocommit = False + +def batch_end(table = None, ipv6=False): + """ Enable autocommit on table and commit changes """ + BATCH_MODE = False + if table: + tables = (table, ) + else: + tables = get_tables(ipv6) + for table in tables: + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.autocommit = True + +def batch_add_chains(table, chains, ipv6=False, flush=True): + """ Add multiple chains to a table """ + iptc_table = _batch_begin_table(table, ipv6) + for chain in chains: + if iptc_table.is_chain(chain): + iptc_chain = Chain(iptc_table, chain) + else: + iptc_chain = iptc_table.create_chain(chain) + if flush: + iptc_chain.flush() + _batch_end_table(table, ipv6) + +def batch_delete_chains(table, chains, ipv6=False): + """ Delete multiple chains of a table """ + iptc_table = _batch_begin_table(table, ipv6) + for chain in chains: + if iptc_table.is_chain(chain): + iptc_chain = Chain(iptc_table, chain) + iptc_chain.flush() + iptc_table.delete_chain(chain) + _batch_end_table(table, ipv6) + +def batch_add_rules(table, batch_rules, ipv6=False): + """ Add multiple rules to a table with format (chain, rule_d, position) """ + iptc_table = _batch_begin_table(table, ipv6) + for (chain, rule_d, position) in batch_rules: + iptc_chain = Chain(iptc_table, chain) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + if position == 0: + # Insert rule in last position -> append + iptc_chain.append_rule(iptc_rule) + elif position > 0: + # Insert rule in given position -> adjusted as iptables CLI + iptc_chain.insert_rule(iptc_rule, position-1) + elif position < 0: + # Insert rule in given position starting from bottom -> not available in iptables CLI + nof_rules = len(iptc_chain.rules) + iptc_chain.insert_rule(iptc_rule, position + nof_rules) + _batch_end_table(table, ipv6) + +def batch_delete_rules(table, batch_rules, ipv6=False, silent=True): + """ Delete multiple rules from table with format (chain, rule_d) """ + try: + iptc_table = _batch_begin_table(table, ipv6) + for (chain, rule_d) in batch_rules: + iptc_chain = Chain(iptc_table, chain) + iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_chain.delete_rule(iptc_rule) + _batch_end_table(table, ipv6) + except Exception as e: + if not silent: raise + + +### INTERNAL FUNCTIONS ### +def _iptc_table_available(table, ipv6=False): + """ Return True if the table is available, False otherwise """ + try: + iptc_table = Table6(table) if ipv6 else Table(table) + return True + except: + return False + +def _iptc_gettables(ipv6=False): + """ Return an updated view of all available iptc_table """ + iptc_cls = Table6 if ipv6 else Table + return [_iptc_gettable(t, ipv6) for t in iptc_cls.ALL if _iptc_table_available(t, ipv6)] + +def _iptc_gettable(table, ipv6=False): + """ Return an updated view of an iptc_table """ + iptc_table = Table6(table) if ipv6 else Table(table) + if BATCH_MODE is False: + iptc_table.commit() + iptc_table.refresh() + return iptc_table + +def _iptc_getchain(table, chain, ipv6=False, silent=False): + """ Return an iptc_chain of an updated table """ + try: + iptc_table = _iptc_gettable(table, ipv6) + if not iptc_table.is_chain(chain): + raise AttributeError('Table <{}> has no chain <{}>'.format(table, chain)) + return Chain(iptc_table, chain) + except Exception as e: + if not silent: raise + +def _iptc_setattr(object, name, value): + # Translate attribute name + name = name.replace('-', '_') + setattr(object, name, value) + +def _iptc_setattr_d(object, value_d): + for name, value in value_d.items(): + _iptc_setattr(object, name, value) + +def _iptc_setrule(iptc_rule, name, value): + _iptc_setattr(iptc_rule, name, value) + +def _iptc_setmatch(iptc_rule, name, value): + # Iterate list/tuple recursively + if isinstance(value, list) or isinstance(value, tuple): + for inner_value in value: + _iptc_setmatch(iptc_rule, name, inner_value) + # Assign dictionary value + elif isinstance(value, dict): + iptc_match = iptc_rule.create_match(name) + _iptc_setattr_d(iptc_match, value) + # Assign value directly + else: + iptc_match = iptc_rule.create_match(name) + _iptc_setattr(iptc_match, name, value) + +def _iptc_settarget(iptc_rule, value): + # Target is dictionary - Use only 1 pair key/value + if isinstance(value, dict): + for k, v in value.items(): + iptc_target = iptc_rule.create_target(k) + _iptc_setattr_d(iptc_target, v) + return + # Simple target + else: + iptc_target = iptc_rule.create_target(value) + +def _encode_iptc_rule(rule_d, ipv6=False): + # Sanity check + assert(isinstance(rule_d, dict)) + # Basic rule attributes + rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') + iptc_rule = Rule6() if ipv6 else Rule() + # Avoid issues with matches that require basic parameters to be configured first + for name in rule_attr: + if name in rule_d: + _iptc_setrule(iptc_rule, name, rule_d[name]) + for name, value in rule_d.items(): + try: + if name in rule_attr: + #_iptc_setrule(iptc_rule, name, value) + continue + elif name == 'target': + _iptc_settarget(iptc_rule, value) + else: + _iptc_setmatch(iptc_rule, name, value) + except Exception as e: + #print('Ignoring unsupported field <{}:{}>'.format(name, value)) + continue + return iptc_rule + +def _decode_iptc_rule(iptc_rule, ipv6=False): + """ Return a dictionary representation of an iptc_rule """ + d = {} + if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0': + d['src'] = iptc_rule.src + elif ipv6==True and iptc_rule.src != '::/0': + d['src'] = iptc_rule.src + if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0': + d['dst'] = iptc_rule.dst.rstrip('/255.255.255.255') + elif ipv6==True and iptc_rule.dst != '::/0': + d['dst'] = iptc_rule.dst.rstrip('/128') + if iptc_rule.protocol != 'ip': + d['protocol'] = iptc_rule.protocol + if iptc_rule.in_interface is not None: + d['in-interface'] = iptc_rule.in_interface + if iptc_rule.out_interface is not None: + d['out-interface'] = iptc_rule.out_interface + if ipv6 == False and iptc_rule.fragment: + d['fragment'] = iptc_rule.fragment + for m in iptc_rule.matches: + if m.name not in d: + d[m.name] = m.get_all_parameters() + elif isinstance(d[m.name], list): + d[m.name].append(m.get_all_parameters()) + else: + d[m.name] = [d[m.name], m.get_all_parameters()] + if iptc_rule.target and iptc_rule.target.name and len(iptc_rule.target.get_all_parameters()): + name = iptc_rule.target.name.replace('-', '_') + d['target'] = {name:iptc_rule.target.get_all_parameters()} + elif iptc_rule.target and iptc_rule.target.name: + d['target'] = iptc_rule.target.name + # Return a filtered dictionary + return _filter_empty_field(d) + +def _batch_begin_table(table, ipv6=False): + """ Disable autocommit on a table """ + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.autocommit = False + return iptc_table + +def _batch_end_table(table, ipv6=False): + """ Enable autocommit on table and commit changes """ + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.autocommit = True + return iptc_table + +def _filter_empty_field(data_d): + """ + Remove empty lists from dictionary values + Before: {'target': {'CHECKSUM': {'checksum-fill': []}}} + After: {'target': {'CHECKSUM': {'checksum-fill': ''}}} + Before: {'tcp': {'dport': ['22']}}} + After: {'tcp': {'dport': '22'}}} + """ + for k, v in data_d.items(): + if isinstance(v, dict): + data_d[k] = _filter_empty_field(v) + elif isinstance(v, list) and len(v) != 0: + v = [_filter_empty_field(_v) if isinstance(_v, dict) else _v for _v in v ] + if isinstance(v, list) and len(v) == 1: + data_d[k] = v.pop() + elif isinstance(v, list) and len(v) == 0: + data_d[k] = '' + return data_d + +### /INTERNAL FUNCTIONS ### + + +# How to use +if __name__== '__main__': + import uuid + # Generate chain name + chain = uuid.uuid4().hex[:10] + + for ipv6 in [False, True]: + table = 'filter' + if not has_chain(table, chain, ipv6): + print('Create new chain {}.{} ipv6={}'.format(table, chain, ipv6)) + add_chain(table, chain, ipv6) + else: + print('Flush existing chain {}.{} ipv6={}'.format(table, chain, ipv6)) + flush_chain(table, chain, ipv6) + + rule_d = {'in_interface':'wan0', + 'protocol':'tcp', + 'tcp': {'tcp-flags': ['FIN,SYN,RST,ACK', 'SYN']}, + 'conntrack': {'ctstate': ['NEW,RELATED']}, + 'comment':{'comment':'New incoming TCP connection'}, + 'mark':[{'mark': '0x1'},{'mark': '0x2'}], + 'target':'ACCEPT'} + # Append new rule + add_rule(table, chain, rule_d, position=0, ipv6=ipv6) + # Show chain rules + print('Display chain {}.{} ipv6={}'.format(table, chain, ipv6)) + print(dump_chain(table, chain, ipv6)) + # Show first rule + print('Display chain 1st rule {}.{} ipv6={}'.format(table, chain, ipv6)) + print(get_rule(table, chain, position=1, ipv6=ipv6)) + # Delete chain + delete_chain(table, chain, ipv6, flush=True) + print('Display table {} ipv6={}'.format(table, ipv6)) + print(dump_table(table, ipv6)) + # Show remaining chains of table + print('Display table chains {} ipv6={}'.format(table, ipv6)) + print(get_chains(table)) From 7111934eb67aa6615e89cd63fb00cfc847c45e07 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 21:47:45 +0000 Subject: [PATCH 084/135] Use raise_exc instead of silent --- iptc/easy.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index bc7021b..4e1136a 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -11,21 +11,21 @@ def flush_all(ipv6=False): for table in get_tables(ipv6): flush_table(table, ipv6) -def flush_table(table, ipv6=False, silent=False): +def flush_table(table, ipv6=False, raise_exc=True): """ Flush a table """ try: iptc_table = _iptc_gettable(table, ipv6) iptc_table.flush() except Exception as e: - if not silent: raise + if raise_exc: raise -def flush_chain(table, chain, ipv6=False, silent=False): +def flush_chain(table, chain, ipv6=False, raise_exc=True): """ Flush a chain in table """ try: iptc_chain = _iptc_getchain(table, chain, ipv6) iptc_chain.flush() except Exception as e: - if not silent: raise + if raise_exc: raise def zero_all(table, ipv6=False): """ Zero all tables """ @@ -53,14 +53,14 @@ def has_rule(table, chain, rule_d, ipv6=False): iptc_rule = _encode_iptc_rule(rule_d, ipv6) return iptc_rule in iptc_chain.rules -def add_chain(table, chain, ipv6=False, silent=False): +def add_chain(table, chain, ipv6=False, raise_exc=True): """ Return True if chain was added successfully to a table, raise Exception otherwise """ try: iptc_table = _iptc_gettable(table, ipv6) iptc_table.create_chain(chain) return True except Exception as e: - if not silent: raise + if raise_exc: raise return False def add_rule(table, chain, rule_d, position=0, ipv6=False): @@ -86,24 +86,24 @@ def insert_rule(table, chain, rule_d, ipv6=False): """ Add a rule to a chain in the 1st position """ add_rule(table, chain, rule_d, position=1, ipv6=ipv6) -def delete_chain(table, chain, ipv6=False, flush=False, silent=False): +def delete_chain(table, chain, ipv6=False, flush=False, raise_exc=True): """ Delete a chain """ try: if flush: - flush_chain(table, chain, ipv6, silent) + flush_chain(table, chain, ipv6, raise_exc) iptc_table = _iptc_gettable(table, ipv6) iptc_table.delete_chain(chain) except Exception as e: - if not silent: raise + if raise_exc: raise -def delete_rule(table, chain, rule_d, ipv6=False, silent=False): +def delete_rule(table, chain, rule_d, ipv6=False, raise_exc=True): """ Delete a rule from a chain """ try: iptc_chain = _iptc_getchain(table, chain, ipv6) iptc_rule = _encode_iptc_rule(rule_d, ipv6) iptc_chain.delete_rule(iptc_rule) except Exception as e: - if not silent: raise + if raise_exc: raise def get_tables(ipv6=False): """ Get all tables """ @@ -115,7 +115,7 @@ def get_chains(table, ipv6=False): iptc_table = _iptc_gettable(table, ipv6) return [iptc_chain.name for iptc_chain in iptc_table.chains] -def get_rule(table, chain, position=0, ipv6=False, silent=False): +def get_rule(table, chain, position=0, ipv6=False, raise_exc=True): """ Get a rule from a chain in a given position. 0=all rules, 1=first, n=nth position """ try: if position == 0: @@ -132,7 +132,7 @@ def get_rule(table, chain, position=0, ipv6=False, silent=False): iptc_rule = iptc_chain.rules[position] return _decode_iptc_rule(iptc_rule, ipv6) except Exception as e: - if not silent: raise + if raise_exc: raise def replace_rule(table, chain, old_rule_d, new_rule_d, ipv6=False): """ Replaces an existing rule of a chain """ @@ -272,7 +272,7 @@ def batch_add_rules(table, batch_rules, ipv6=False): iptc_chain.insert_rule(iptc_rule, position + nof_rules) _batch_end_table(table, ipv6) -def batch_delete_rules(table, batch_rules, ipv6=False, silent=True): +def batch_delete_rules(table, batch_rules, ipv6=False, raise_exc=True): """ Delete multiple rules from table with format (chain, rule_d) """ try: iptc_table = _batch_begin_table(table, ipv6) @@ -282,7 +282,7 @@ def batch_delete_rules(table, batch_rules, ipv6=False, silent=True): iptc_chain.delete_rule(iptc_rule) _batch_end_table(table, ipv6) except Exception as e: - if not silent: raise + if raise_exc: raise ### INTERNAL FUNCTIONS ### @@ -307,7 +307,7 @@ def _iptc_gettable(table, ipv6=False): iptc_table.refresh() return iptc_table -def _iptc_getchain(table, chain, ipv6=False, silent=False): +def _iptc_getchain(table, chain, ipv6=False, raise_exc=True): """ Return an iptc_chain of an updated table """ try: iptc_table = _iptc_gettable(table, ipv6) @@ -315,7 +315,7 @@ def _iptc_getchain(table, chain, ipv6=False, silent=False): raise AttributeError('Table <{}> has no chain <{}>'.format(table, chain)) return Chain(iptc_table, chain) except Exception as e: - if not silent: raise + if raise_exc: raise def _iptc_setattr(object, name, value): # Translate attribute name From 05f8b89a29e9034c8f3f74861967bd85b9193508 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 21:48:15 +0000 Subject: [PATCH 085/135] Use more pythonic way for dump_() functions --- iptc/easy.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 4e1136a..137c3a0 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -188,26 +188,16 @@ def test_target(name, value, ipv6=False): def dump_all(ipv6=False): """ Return a dictionary representation of all tables """ - d = {} - for table in get_tables(ipv6): - d[table] = dump_table(table, ipv6) - return d + return {table: dump_table(table, ipv6) for table in get_tables(ipv6)} def dump_table(table, ipv6=False): """ Return a dictionary representation of a table """ - d = {} - iptc_table = _iptc_gettable(table, ipv6) - for iptc_chain in iptc_table.chains: - d[iptc_chain.name] = dump_chain(iptc_table.name, iptc_chain.name, ipv6) - return d + return {chain: dump_chain(table, chain, ipv6) for chain in get_chains(table, ipv6)} def dump_chain(table, chain, ipv6=False): """ Return a list with the dictionary representation of the rules of a table """ - l = [] iptc_chain = _iptc_getchain(table, chain, ipv6) - for i, iptc_rule in enumerate(iptc_chain.rules): - l.append(_decode_iptc_rule(iptc_rule, ipv6)) - return l + return [_decode_iptc_rule(iptc_rule, ipv6) for iptc_rule in iptc_chain.rules] def batch_begin(table = None, ipv6=False): From dcf72b4552c37614a76d5ad3a1e134884d19e6bd Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:17:08 +0000 Subject: [PATCH 086/135] Use get_tables() instead of table.ALL --- iptc/easy.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 137c3a0..015583b 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -29,8 +29,7 @@ def flush_chain(table, chain, ipv6=False, raise_exc=True): def zero_all(table, ipv6=False): """ Zero all tables """ - table_cls = Table6 if ipv6 else Table - for table in table_cls.ALL: + for table in get_tables(ipv6): zero_table(table, ipv6) def zero_table(table, ipv6=False): From 4791e47925e875d5f33adbe22bb8a093f48c8bd0 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:18:15 +0000 Subject: [PATCH 087/135] Make public [encode,decode]_ipct_rule functions --- iptc/easy.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 015583b..e11e4b0 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -49,7 +49,7 @@ def has_chain(table, chain, ipv6=False): def has_rule(table, chain, rule_d, ipv6=False): """ Return True if rule exists in chain False otherwise """ iptc_chain = _iptc_getchain(table, chain, ipv6) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) return iptc_rule in iptc_chain.rules def add_chain(table, chain, ipv6=False, raise_exc=True): @@ -65,7 +65,7 @@ def add_chain(table, chain, ipv6=False, raise_exc=True): def add_rule(table, chain, rule_d, position=0, ipv6=False): """ Add a rule to a chain in a given position. 0=append, 1=first, n=nth position """ iptc_chain = _iptc_getchain(table, chain, ipv6) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) if position == 0: # Insert rule in last position -> append iptc_chain.append_rule(iptc_rule) @@ -99,7 +99,7 @@ def delete_rule(table, chain, rule_d, ipv6=False, raise_exc=True): """ Delete a rule from a chain """ try: iptc_chain = _iptc_getchain(table, chain, ipv6) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) iptc_chain.delete_rule(iptc_rule) except Exception as e: if raise_exc: raise @@ -124,20 +124,20 @@ def get_rule(table, chain, position=0, ipv6=False, raise_exc=True): # Return specific rule by position iptc_chain = _iptc_getchain(table, chain, ipv6) iptc_rule = iptc_chain.rules[position - 1] - return _decode_iptc_rule(iptc_rule, ipv6) + return decode_iptc_rule(iptc_rule, ipv6) elif position < 0: # Return last rule -> not available in iptables CLI iptc_chain = _iptc_getchain(table, chain, ipv6) iptc_rule = iptc_chain.rules[position] - return _decode_iptc_rule(iptc_rule, ipv6) + return decode_iptc_rule(iptc_rule, ipv6) except Exception as e: if raise_exc: raise def replace_rule(table, chain, old_rule_d, new_rule_d, ipv6=False): """ Replaces an existing rule of a chain """ iptc_chain = _iptc_getchain(table, chain, ipv6) - iptc_old_rule = _encode_iptc_rule(old_rule_d, ipv6) - iptc_new_rule = _encode_iptc_rule(new_rule_d, ipv6) + iptc_old_rule = encode_iptc_rule(old_rule_d, ipv6) + iptc_new_rule = encode_iptc_rule(new_rule_d, ipv6) iptc_chain.replace_rule(iptc_new_rule, iptc_chain.rules.index(iptc_old_rule)) def get_rule_statistics(table, chain, rule_d, ipv6=False): @@ -145,7 +145,7 @@ def get_rule_statistics(table, chain, rule_d, ipv6=False): if not has_rule(table, chain, rule_d, ipv6): raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d)) iptc_chain = _iptc_getchain(table, chain, ipv6) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) iptc_rule_index = iptc_chain.rules.index(iptc_rule) return iptc_chain.rules[iptc_rule_index].get_counters() @@ -154,14 +154,14 @@ def get_rule_position(table, chain, rule_d, ipv6=False): if not has_rule(table, chain, rule_d): raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d)) iptc_chain = _iptc_getchain(table, chain, ipv6) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) return iptc_chain.rules.index(iptc_rule) def test_rule(rule_d, ipv6=False): """ Return True if the rule is a well-formed dictionary, False otherwise """ try: - _encode_iptc_rule(rule_d, ipv6) + encode_iptc_rule(rule_d, ipv6) return True except: return False @@ -196,7 +196,7 @@ def dump_table(table, ipv6=False): def dump_chain(table, chain, ipv6=False): """ Return a list with the dictionary representation of the rules of a table """ iptc_chain = _iptc_getchain(table, chain, ipv6) - return [_decode_iptc_rule(iptc_rule, ipv6) for iptc_rule in iptc_chain.rules] + return [decode_iptc_rule(iptc_rule, ipv6) for iptc_rule in iptc_chain.rules] def batch_begin(table = None, ipv6=False): @@ -248,7 +248,7 @@ def batch_add_rules(table, batch_rules, ipv6=False): iptc_table = _batch_begin_table(table, ipv6) for (chain, rule_d, position) in batch_rules: iptc_chain = Chain(iptc_table, chain) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) if position == 0: # Insert rule in last position -> append iptc_chain.append_rule(iptc_rule) @@ -267,7 +267,7 @@ def batch_delete_rules(table, batch_rules, ipv6=False, raise_exc=True): iptc_table = _batch_begin_table(table, ipv6) for (chain, rule_d) in batch_rules: iptc_chain = Chain(iptc_table, chain) - iptc_rule = _encode_iptc_rule(rule_d, ipv6) + iptc_rule = encode_iptc_rule(rule_d, ipv6) iptc_chain.delete_rule(iptc_rule) _batch_end_table(table, ipv6) except Exception as e: @@ -343,7 +343,7 @@ def _iptc_settarget(iptc_rule, value): else: iptc_target = iptc_rule.create_target(value) -def _encode_iptc_rule(rule_d, ipv6=False): +def encode_iptc_rule(rule_d, ipv6=False): # Sanity check assert(isinstance(rule_d, dict)) # Basic rule attributes @@ -367,7 +367,7 @@ def _encode_iptc_rule(rule_d, ipv6=False): continue return iptc_rule -def _decode_iptc_rule(iptc_rule, ipv6=False): +def decode_iptc_rule(iptc_rule, ipv6=False): """ Return a dictionary representation of an iptc_rule """ d = {} if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0': From 3eb7d2340b93e9993b0413b6d26963de147fe517 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:35:20 +0000 Subject: [PATCH 088/135] Renamed get_rule_counters function --- iptc/easy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/easy.py b/iptc/easy.py index e11e4b0..62338f4 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -140,7 +140,7 @@ def replace_rule(table, chain, old_rule_d, new_rule_d, ipv6=False): iptc_new_rule = encode_iptc_rule(new_rule_d, ipv6) iptc_chain.replace_rule(iptc_new_rule, iptc_chain.rules.index(iptc_old_rule)) -def get_rule_statistics(table, chain, rule_d, ipv6=False): +def get_rule_counters(table, chain, rule_d, ipv6=False): """ Return a tuple with the rule counters (numberOfBytes, numberOfPackets) """ if not has_rule(table, chain, rule_d, ipv6): raise AttributeError('Chain <{}@{}> has no rule <{}>'.format(chain, table, rule_d)) From 0e6946287e7b2e86f2ad1598b5337c79d59f32a9 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:35:40 +0000 Subject: [PATCH 089/135] Add set/get policy functions --- iptc/easy.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/iptc/easy.py b/iptc/easy.py index 62338f4..8ce1f54 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -185,6 +185,17 @@ def test_target(name, value, ipv6=False): return False +def get_policy(table, chain, ipv6=False): + """ Return the default policy of chain in a table """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + return iptc_chain.get_policy().name + +def set_policy(table, chain, policy='ACCEPT', ipv6=False): + """ Set the default policy of chain in a table """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + iptc_chain.set_policy(policy) + + def dump_all(ipv6=False): """ Return a dictionary representation of all tables """ return {table: dump_table(table, ipv6) for table in get_tables(ipv6)} From 50074e9ab5762c84188428480c8c4c8131b5d60d Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:37:26 +0000 Subject: [PATCH 090/135] Removed if __name__== '__main__' section --- iptc/easy.py | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 8ce1f54..c09e197 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -444,42 +444,3 @@ def _filter_empty_field(data_d): return data_d ### /INTERNAL FUNCTIONS ### - - -# How to use -if __name__== '__main__': - import uuid - # Generate chain name - chain = uuid.uuid4().hex[:10] - - for ipv6 in [False, True]: - table = 'filter' - if not has_chain(table, chain, ipv6): - print('Create new chain {}.{} ipv6={}'.format(table, chain, ipv6)) - add_chain(table, chain, ipv6) - else: - print('Flush existing chain {}.{} ipv6={}'.format(table, chain, ipv6)) - flush_chain(table, chain, ipv6) - - rule_d = {'in_interface':'wan0', - 'protocol':'tcp', - 'tcp': {'tcp-flags': ['FIN,SYN,RST,ACK', 'SYN']}, - 'conntrack': {'ctstate': ['NEW,RELATED']}, - 'comment':{'comment':'New incoming TCP connection'}, - 'mark':[{'mark': '0x1'},{'mark': '0x2'}], - 'target':'ACCEPT'} - # Append new rule - add_rule(table, chain, rule_d, position=0, ipv6=ipv6) - # Show chain rules - print('Display chain {}.{} ipv6={}'.format(table, chain, ipv6)) - print(dump_chain(table, chain, ipv6)) - # Show first rule - print('Display chain 1st rule {}.{} ipv6={}'.format(table, chain, ipv6)) - print(get_rule(table, chain, position=1, ipv6=ipv6)) - # Delete chain - delete_chain(table, chain, ipv6, flush=True) - print('Display table {} ipv6={}'.format(table, ipv6)) - print(dump_table(table, ipv6)) - # Show remaining chains of table - print('Display table chains {} ipv6={}'.format(table, ipv6)) - print(get_chains(table)) From 328146f05c7fc112c4c7c47d1264e438c632d845 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:39:03 +0000 Subject: [PATCH 091/135] Make _BATCH_MODE private --- iptc/easy.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index c09e197..1204a05 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -1,10 +1,9 @@ -# TODO -# - Expose POLICY of a table +# -*- coding: utf-8 -*- from .ip4tc import Rule, Table, Chain, IPTCError from .ip6tc import Rule6, Table6 -BATCH_MODE = False +_BATCH_MODE = False def flush_all(ipv6=False): """ Flush all available tables """ @@ -212,7 +211,7 @@ def dump_chain(table, chain, ipv6=False): def batch_begin(table = None, ipv6=False): """ Disable autocommit on a table """ - BATCH_MODE = True + _BATCH_MODE = True if table: tables = (table, ) else: @@ -223,7 +222,7 @@ def batch_begin(table = None, ipv6=False): def batch_end(table = None, ipv6=False): """ Enable autocommit on table and commit changes """ - BATCH_MODE = False + _BATCH_MODE = False if table: tables = (table, ) else: @@ -302,7 +301,7 @@ def _iptc_gettables(ipv6=False): def _iptc_gettable(table, ipv6=False): """ Return an updated view of an iptc_table """ iptc_table = Table6(table) if ipv6 else Table(table) - if BATCH_MODE is False: + if _BATCH_MODE is False: iptc_table.commit() iptc_table.refresh() return iptc_table From dd3d92d2ee3459101e45e81428f139009910ff34 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 26 Mar 2019 22:39:42 +0000 Subject: [PATCH 092/135] Add TODOs --- iptc/easy.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/iptc/easy.py b/iptc/easy.py index 1204a05..a64d846 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -1,5 +1,9 @@ # -*- coding: utf-8 -*- +# TODO: +# - Add documentation +# - Add HowToUse examples + from .ip4tc import Rule, Table, Chain, IPTCError from .ip6tc import Rule6, Table6 From b1ad40a8eec74c36fcbc95de94af311145ec2d69 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 9 Apr 2019 19:45:08 +0000 Subject: [PATCH 093/135] Add Subnet Mask conversion --- iptc/easy.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index a64d846..485955e 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -385,11 +385,15 @@ def decode_iptc_rule(iptc_rule, ipv6=False): """ Return a dictionary representation of an iptc_rule """ d = {} if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0': - d['src'] = iptc_rule.src + _ip, _netmask = iptc_rule.src.split('/') + _netmask = _netmask_v4_to_cidr(_netmask) + d['src'] = '{}/{}'.format(_ip, _netmask) elif ipv6==True and iptc_rule.src != '::/0': - d['src'] = iptc_rule.src + d['src'] = iptc_rule.src.rstrip('/128') if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0': - d['dst'] = iptc_rule.dst.rstrip('/255.255.255.255') + _ip, _netmask = iptc_rule.dst.split('/') + _netmask = _netmask_v4_to_cidr(_netmask) + d['dst'] = '{}/{}'.format(_ip, _netmask) elif ipv6==True and iptc_rule.dst != '::/0': d['dst'] = iptc_rule.dst.rstrip('/128') if iptc_rule.protocol != 'ip': @@ -446,4 +450,8 @@ def _filter_empty_field(data_d): data_d[k] = '' return data_d +def _netmask_v4_to_cidr(netmask_addr): + # Implement Subnet Mask conversion without dependencies + return sum([bin(int(x)).count('1') for x in netmask_addr.split('.')]) + ### /INTERNAL FUNCTIONS ### From cccb2741987358d70eb5ebccb0d77fdc6dd1b78a Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 9 Apr 2019 20:04:55 +0000 Subject: [PATCH 094/135] Enforce CIDR in IPv6 addresses --- iptc/easy.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 485955e..def9997 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -382,20 +382,21 @@ def encode_iptc_rule(rule_d, ipv6=False): return iptc_rule def decode_iptc_rule(iptc_rule, ipv6=False): - """ Return a dictionary representation of an iptc_rule """ + """ Return a dictionary representation of an iptc_rule + Note: host IP addresses are appended their corresponding CIDR """ d = {} if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0': _ip, _netmask = iptc_rule.src.split('/') _netmask = _netmask_v4_to_cidr(_netmask) d['src'] = '{}/{}'.format(_ip, _netmask) elif ipv6==True and iptc_rule.src != '::/0': - d['src'] = iptc_rule.src.rstrip('/128') + d['src'] = iptc_rule.src if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0': _ip, _netmask = iptc_rule.dst.split('/') _netmask = _netmask_v4_to_cidr(_netmask) d['dst'] = '{}/{}'.format(_ip, _netmask) elif ipv6==True and iptc_rule.dst != '::/0': - d['dst'] = iptc_rule.dst.rstrip('/128') + d['dst'] = iptc_rule.dst if iptc_rule.protocol != 'ip': d['protocol'] = iptc_rule.protocol if iptc_rule.in_interface is not None: From b898fa44dc532c2f0adf996e43ccfcb8ff1b65ba Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 9 Apr 2019 20:37:12 +0000 Subject: [PATCH 095/135] Added documentation. Cleanup --- README.md | 40 ++++++++++++++ doc/examples.rst | 29 ++++++++++- iptc/easy.py | 126 ++++++++++++++++++++++----------------------- iptc/ip4tc.py | 119 +----------------------------------------- tests/test_iptc.py | 18 +++---- 5 files changed, 140 insertions(+), 192 deletions(-) diff --git a/README.md b/README.md index 60ffa3c..31d2a3a 100755 --- a/README.md +++ b/README.md @@ -135,6 +135,31 @@ Full documentation with API reference is available Examples ======== +High level abstractions +----------------------- + +``python-iptables`` implements a low-level interface that tries to closely +match the underlying C libraries. The module ``iptc.easy`` improves the +usability of the library by providing a rich set of high-level functions +designed to simplify the interaction with the library, for example:: + + >>> import iptc + >>> iptc.easy.dump_table('nat', ipv6=False) + {'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []} + >>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False) + [{'comment': {'comment': 'DNS traffic to Google'}, + 'dst': '8.8.8.8/32', + 'protocol': 'udp', + 'target': 'ACCEPT', + 'udp': {'dport': '53'}}] + >>> iptc.easy.add_chain('filter', 'TestChain') + True + >>> rule_d = {'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}} + >>> iptc.easy.insert_rule('filter', 'TestChain', rule_d) + >>> iptc.easy.dump_chain('filter', 'TestChain') + [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}] + >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True) + Rules ----- @@ -546,6 +571,21 @@ or more rules, than commit it: The drawback is that Table is a singleton, and if you disable autocommit, it will be disabled for all instances of that Table. +Easy rules with dictionaries +---------------------------- +To simplify operations with ``python-iptables`` rules we have included support to define and convert Rules object into python dictionaries. + + >>> import iptc + >>> table = iptc.Table(iptc.Table.FILTER) + >>> chain = iptc.Chain(table, "INPUT") + >>> # Create an iptc.Rule object from dictionary + >>> rule_d = {'comment': {'comment': 'Match tcp.22'}, 'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}} + >>> rule = iptc.easy.encode_iptc_rule(rule_d) + >>> # Obtain a dictionary representation from the iptc.Rule + >>> iptc.easy.decode_iptc_rule(rule) + {'tcp': {'dport': '22'}, 'protocol': 'tcp', 'comment': {'comment': 'Match tcp.22'}, 'target': 'ACCEPT'} + + Known Issues ============ diff --git a/doc/examples.rst b/doc/examples.rst index 1a8fc63..2f6d9a4 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -1,6 +1,31 @@ Examples ======== +High level abstractions +----------------------- + +``python-iptables`` implements a low-level interface that tries to closely +match the underlying C libraries. The module ``iptc.easy`` improves the +usability of the library by providing a rich set of high-level functions +designed to simplify the interaction with the library, for example:: + + >>> import iptc + >>> iptc.easy.dump_table('nat', ipv6=False) + {'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []} + >>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False) + [{'comment': {'comment': 'DNS traffic to Google'}, + 'dst': '8.8.8.8/32', + 'protocol': 'udp', + 'target': 'ACCEPT', + 'udp': {'dport': '53'}}] + >>> iptc.easy.add_chain('filter', 'TestChain') + True + >>> rule_d = {'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}} + >>> iptc.easy.insert_rule('filter', 'TestChain', rule_d) + >>> iptc.easy.dump_chain('filter', 'TestChain') + [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}] + >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True) + Rules ----- @@ -419,9 +444,9 @@ To simplify operations with ``python-iptables`` rules we have included support t >>> chain = iptc.Chain(table, "INPUT") >>> # Create an iptc.Rule object from dictionary >>> rule_d = {'comment': {'comment': 'Match tcp.22'}, 'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}} - >>> rule = iptc.Rule.from_dict(rule_d) + >>> rule = iptc.easy.encode_iptc_rule(rule_d) >>> # Obtain a dictionary representation from the iptc.Rule - >>> rule.to_dict() + >>> iptc.easy.decode_iptc_rule(rule) {'tcp': {'dport': '22'}, 'protocol': 'tcp', 'comment': {'comment': 'Match tcp.22'}, 'target': 'ACCEPT'} diff --git a/iptc/easy.py b/iptc/easy.py index def9997..22f9743 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -288,6 +288,69 @@ def batch_delete_rules(table, batch_rules, ipv6=False, raise_exc=True): if raise_exc: raise +def encode_iptc_rule(rule_d, ipv6=False): + """ Return a Rule(6) object from the input dictionary """ + # Sanity check + assert(isinstance(rule_d, dict)) + # Basic rule attributes + rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') + iptc_rule = Rule6() if ipv6 else Rule() + # Avoid issues with matches that require basic parameters to be configured first + for name in rule_attr: + if name in rule_d: + _iptc_setrule(iptc_rule, name, rule_d[name]) + for name, value in rule_d.items(): + try: + if name in rule_attr: + continue + elif name == 'target': + _iptc_settarget(iptc_rule, value) + else: + _iptc_setmatch(iptc_rule, name, value) + except Exception as e: + #print('Ignoring unsupported field <{}:{}>'.format(name, value)) + continue + return iptc_rule + +def decode_iptc_rule(iptc_rule, ipv6=False): + """ Return a dictionary representation of the Rule(6) object + Note: host IP addresses are appended their corresponding CIDR """ + d = {} + if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0': + _ip, _netmask = iptc_rule.src.split('/') + _netmask = _netmask_v4_to_cidr(_netmask) + d['src'] = '{}/{}'.format(_ip, _netmask) + elif ipv6==True and iptc_rule.src != '::/0': + d['src'] = iptc_rule.src + if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0': + _ip, _netmask = iptc_rule.dst.split('/') + _netmask = _netmask_v4_to_cidr(_netmask) + d['dst'] = '{}/{}'.format(_ip, _netmask) + elif ipv6==True and iptc_rule.dst != '::/0': + d['dst'] = iptc_rule.dst + if iptc_rule.protocol != 'ip': + d['protocol'] = iptc_rule.protocol + if iptc_rule.in_interface is not None: + d['in-interface'] = iptc_rule.in_interface + if iptc_rule.out_interface is not None: + d['out-interface'] = iptc_rule.out_interface + if ipv6 == False and iptc_rule.fragment: + d['fragment'] = iptc_rule.fragment + for m in iptc_rule.matches: + if m.name not in d: + d[m.name] = m.get_all_parameters() + elif isinstance(d[m.name], list): + d[m.name].append(m.get_all_parameters()) + else: + d[m.name] = [d[m.name], m.get_all_parameters()] + if iptc_rule.target and iptc_rule.target.name and len(iptc_rule.target.get_all_parameters()): + name = iptc_rule.target.name.replace('-', '_') + d['target'] = {name:iptc_rule.target.get_all_parameters()} + elif iptc_rule.target and iptc_rule.target.name: + d['target'] = iptc_rule.target.name + # Return a filtered dictionary + return _filter_empty_field(d) + ### INTERNAL FUNCTIONS ### def _iptc_table_available(table, ipv6=False): """ Return True if the table is available, False otherwise """ @@ -357,69 +420,6 @@ def _iptc_settarget(iptc_rule, value): else: iptc_target = iptc_rule.create_target(value) -def encode_iptc_rule(rule_d, ipv6=False): - # Sanity check - assert(isinstance(rule_d, dict)) - # Basic rule attributes - rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') - iptc_rule = Rule6() if ipv6 else Rule() - # Avoid issues with matches that require basic parameters to be configured first - for name in rule_attr: - if name in rule_d: - _iptc_setrule(iptc_rule, name, rule_d[name]) - for name, value in rule_d.items(): - try: - if name in rule_attr: - #_iptc_setrule(iptc_rule, name, value) - continue - elif name == 'target': - _iptc_settarget(iptc_rule, value) - else: - _iptc_setmatch(iptc_rule, name, value) - except Exception as e: - #print('Ignoring unsupported field <{}:{}>'.format(name, value)) - continue - return iptc_rule - -def decode_iptc_rule(iptc_rule, ipv6=False): - """ Return a dictionary representation of an iptc_rule - Note: host IP addresses are appended their corresponding CIDR """ - d = {} - if ipv6==False and iptc_rule.src != '0.0.0.0/0.0.0.0': - _ip, _netmask = iptc_rule.src.split('/') - _netmask = _netmask_v4_to_cidr(_netmask) - d['src'] = '{}/{}'.format(_ip, _netmask) - elif ipv6==True and iptc_rule.src != '::/0': - d['src'] = iptc_rule.src - if ipv6==False and iptc_rule.dst != '0.0.0.0/0.0.0.0': - _ip, _netmask = iptc_rule.dst.split('/') - _netmask = _netmask_v4_to_cidr(_netmask) - d['dst'] = '{}/{}'.format(_ip, _netmask) - elif ipv6==True and iptc_rule.dst != '::/0': - d['dst'] = iptc_rule.dst - if iptc_rule.protocol != 'ip': - d['protocol'] = iptc_rule.protocol - if iptc_rule.in_interface is not None: - d['in-interface'] = iptc_rule.in_interface - if iptc_rule.out_interface is not None: - d['out-interface'] = iptc_rule.out_interface - if ipv6 == False and iptc_rule.fragment: - d['fragment'] = iptc_rule.fragment - for m in iptc_rule.matches: - if m.name not in d: - d[m.name] = m.get_all_parameters() - elif isinstance(d[m.name], list): - d[m.name].append(m.get_all_parameters()) - else: - d[m.name] = [d[m.name], m.get_all_parameters()] - if iptc_rule.target and iptc_rule.target.name and len(iptc_rule.target.get_all_parameters()): - name = iptc_rule.target.name.replace('-', '_') - d['target'] = {name:iptc_rule.target.get_all_parameters()} - elif iptc_rule.target and iptc_rule.target.name: - d['target'] = iptc_rule.target.name - # Return a filtered dictionary - return _filter_empty_field(d) - def _batch_begin_table(table, ipv6=False): """ Disable autocommit on a table """ iptc_table = _iptc_gettable(table, ipv6) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index f68b259..29d77ea 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -10,7 +10,7 @@ import weakref from .util import find_library, load_kernel -from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, NFPROTO_IPV6, XTablesError, xtables, +from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables, xt_align, xt_counters, xt_entry_target, xt_entry_match) __all__ = ["Table", "Chain", "Rule", "Match", "Target", "Policy", "IPTCError"] @@ -1389,123 +1389,6 @@ def _get_mask(self): mask = property(_get_mask) """This is the raw mask buffer as iptables uses it when removing rules.""" - @classmethod - def from_dict(cls, rule_d): - """Generate a Rule(6) object from the input dictionary.""" - # Sanity check - assert(isinstance(rule_d, dict)) - # Basic rule attributes - rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') - iptc_rule = cls() - # Avoid issues with matches that require basic parameters to be configured first - for name in rule_attr: - if name in rule_d: - _iptc_setrule(iptc_rule, name, rule_d[name]) - for name, value in rule_d.items(): - try: - if name in rule_attr: - #_iptc_setrule(iptc_rule, name, value) - continue - elif name == 'target': - _iptc_settarget(iptc_rule, value) - else: - _iptc_setmatch(iptc_rule, name, value) - except Exception as e: - #print('Ignoring unsupported field <{}:{}>'.format(name, value)) - continue - return iptc_rule - - def to_dict(self): - """Generate a dictionary representation of the Rule(6) object.""" - d = {} - if self.nfproto==NFPROTO_IPV4 and self.src != '0.0.0.0/0.0.0.0': - d['src'] = self.src - elif self.nfproto==NFPROTO_IPV6 and self.src != '::/0': - d['src'] = self.src - if self.nfproto==NFPROTO_IPV4 and self.dst != '0.0.0.0/0.0.0.0': - d['dst'] = self.dst - elif self.nfproto==NFPROTO_IPV6 and self.dst != '::/0': - d['dst'] = self.dst - if self.protocol != 'ip': - d['protocol'] = self.protocol - if self.in_interface is not None: - d['in-interface'] = self.in_interface - if self.out_interface is not None: - d['out-interface'] = self.out_interface - if self.nfproto==NFPROTO_IPV4 and self.fragment: - d['fragment'] = self.fragment - for m in self.matches: - if m.name not in d: - d[m.name] = m.get_all_parameters() - elif isinstance(d[m.name], list): - d[m.name].append(m.get_all_parameters()) - else: - d[m.name] = [d[m.name], m.get_all_parameters()] - if self.target and self.target.name and len(self.target.get_all_parameters()): - name = self.target.name.replace('-', '_') - d['target'] = {name:self.target.get_all_parameters()} - elif self.target and self.target.name: - d['target'] = self.target.name - # Return a filtered dictionary - return _filter_empty_field(d) - -# Helper functions for dictionary operations over Rule(6) objects -def _iptc_setattr(object, name, value): - # Translate attribute name - name = name.replace('-', '_') - setattr(object, name, value) - -def _iptc_setattr_d(object, value_d): - for name, value in value_d.items(): - _iptc_setattr(object, name, value) - -def _iptc_setrule(iptc_rule, name, value): - _iptc_setattr(iptc_rule, name, value) - -def _iptc_setmatch(iptc_rule, name, value): - # Iterate list/tuple recursively - if isinstance(value, list) or isinstance(value, tuple): - for inner_value in value: - _iptc_setmatch(iptc_rule, name, inner_value) - # Assign dictionary value - elif isinstance(value, dict): - iptc_match = iptc_rule.create_match(name) - _iptc_setattr_d(iptc_match, value) - # Assign value directly - else: - iptc_match = iptc_rule.create_match(name) - _iptc_setattr(iptc_match, name, value) - -def _iptc_settarget(iptc_rule, value): - # Target is dictionary - Use only 1 pair key/value - if isinstance(value, dict): - for k, v in value.items(): - iptc_target = iptc_rule.create_target(k) - _iptc_setattr_d(iptc_target, v) - return - # Simple target - else: - iptc_target = iptc_rule.create_target(value) - -def _filter_empty_field(data_d): - """ - Remove empty lists from dictionary values - Before: {'target': {'CHECKSUM': {'checksum-fill': []}}} - After: {'target': {'CHECKSUM': {'checksum-fill': ''}}} - Before: {'tcp': {'dport': ['22']}}} - After: {'tcp': {'dport': '22'}}} - """ - for k, v in data_d.items(): - if isinstance(v, dict): - data_d[k] = _filter_empty_field(v) - elif isinstance(v, list) and len(v) != 0: - v = [_filter_empty_field(_v) if isinstance(_v, dict) else _v for _v in v ] - if isinstance(v, list) and len(v) == 1: - data_d[k] = v.pop() - elif isinstance(v, list) and len(v) == 0: - data_d[k] = '' - return data_d - class Chain(object): """Rules are contained by chains. diff --git a/tests/test_iptc.py b/tests/test_iptc.py index c01abef..519e228 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -577,19 +577,19 @@ def test_rule_insert(self): def test_rule_to_dict(self): rule = iptc.Rule6() rule.protocol = "tcp" - rule.src = "::1" + rule.src = "::1/128" target = iptc.Target(rule, "ACCEPT") rule.target = target - rule_d = rule.to_dict() + rule_d = iptc.easy.decode_iptc_rule(rule, ipv6=True) self.assertEqual(rule_d, {"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}) def test_rule_from_dict(self): rule = iptc.Rule6() rule.protocol = "tcp" - rule.src = "::1" + rule.src = "::1/128" target = iptc.Target(rule, "ACCEPT") rule.target = target - rule2 = iptc.Rule6.from_dict({"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}) + rule2 = iptc.easy.encode_iptc_rule({"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}, ipv6=True) self.assertEqual(rule, rule2) class TestRule(unittest.TestCase): @@ -935,19 +935,19 @@ def test_rule_delete_nat(self): def test_rule_to_dict(self): rule = iptc.Rule() rule.protocol = "tcp" - rule.src = "127.0.0.1" + rule.src = "127.0.0.1/32" target = iptc.Target(rule, "ACCEPT") rule.target = target - rule_d = rule.to_dict() - self.assertEqual(rule_d, {"protocol": "tcp", "src": "127.0.0.1/255.255.255.255", "target": "ACCEPT"}) + rule_d = iptc.easy.decode_iptc_rule(rule) + self.assertEqual(rule_d, {"protocol": "tcp", "src": "127.0.0.1/32", "target": "ACCEPT"}) def test_rule_from_dict(self): rule = iptc.Rule() rule.protocol = "tcp" - rule.src = "127.0.0.1" + rule.src = "127.0.0.1/32" target = iptc.Target(rule, "ACCEPT") rule.target = target - rule2 = iptc.Rule.from_dict({"protocol": "tcp", "src": "127.0.0.1/255.255.255.255", "target": "ACCEPT"}) + rule2 = iptc.easy.encode_iptc_rule({"protocol": "tcp", "src": "127.0.0.1/32", "target": "ACCEPT"}) self.assertEqual(rule, rule2) def suite(): From ce1936f7f4c6de81944cd325db80e9f5ce00330f Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Sat, 18 May 2019 18:26:21 +0000 Subject: [PATCH 096/135] Fix comparison of Match instances and update test_matches file --- iptc/ip4tc.py | 4 +--- tests/test_matches.py | 54 ++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 29d77ea..0bec5a5 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -574,9 +574,7 @@ def _check_alias(self): def __eq__(self, match): basesz = ct.sizeof(xt_entry_match) - if (self.match.u.match_size == match.match.u.match_size and - self.match.u.user.name == match.match.u.user.name and - self.match.u.user.revision == match.match.u.user.revision and + if (self.name == match.name and self.match_buf[basesz:self.usersize] == match.match_buf[basesz:match.usersize]): return True diff --git a/tests/test_matches.py b/tests/test_matches.py index 1bf86dd..063cfb4 100755 --- a/tests/test_matches.py +++ b/tests/test_matches.py @@ -19,7 +19,7 @@ def test_match_create(self): match = rule.create_match("udp") for m in rule.matches: - self.failUnless(m == match) + self.assertEqual(m, match) # check that we can change match parameters after creation match.sport = "12345:55555" @@ -29,7 +29,7 @@ def test_match_create(self): m.sport = "12345:55555" m.dport = "!33333" - self.failUnless(m == match) + self.assertEqual(m, match) def test_match_compare(self): m1 = iptc.Match(iptc.Rule(), "udp") @@ -40,28 +40,28 @@ def test_match_compare(self): m2.sport = "12345:55555" m2.dport = "!33333" - self.failUnless(m1 == m2) + self.assertEqual(m1, m2) m2.reset() m2.sport = "12345:55555" m2.dport = "33333" - self.failIf(m1 == m2) + self.assertNotEqual(m1, m2) def test_match_parameters(self): m = iptc.Match(iptc.Rule(), "udp") m.sport = "12345:55555" m.dport = "!33333" - self.failUnless(len(m.parameters) == 2) + self.assertEqual(len(m.parameters), 2) for p in m.parameters: - self.failUnless(p == "sport" or p == "dport") + self.assertTrue(p == "sport" or p == "dport") - self.failUnless(m.parameters["sport"] == "12345:55555") - self.failUnless(m.parameters["dport"] == "!33333") + self.assertEqual(m.parameters["sport"], "12345:55555") + self.assertEqual(m.parameters["dport"], "!33333") m.reset() - self.failUnless(len(m.parameters) == 0) + self.assertEqual(len(m.parameters), 0) def test_get_all_parameters(self): m = iptc.Match(iptc.Rule(), "udp") @@ -69,8 +69,8 @@ def test_get_all_parameters(self): m.dport = "!33333" params = m.get_all_parameters() - self.assertEquals(set(params['sport']), set(['12345:55555'])) - self.assertEquals(set(params['dport']), set(['!', '33333'])) + self.assertEqual(set(params['sport']), set(['12345:55555'])) + self.assertEqual(set(params['dport']), set(['!', '33333'])) class TestMultiportMatch(unittest.TestCase): @@ -103,14 +103,14 @@ def test_multiport(self): self.chain.insert_rule(self.rule) rule = self.chain.rules[0] match = rule.matches[0] - self.assertEquals(match.dports, '1111,2222') + self.assertEqual(match.dports, '1111,2222') def test_unicode_multiport(self): self.match.dports = u'1111,2222' self.chain.insert_rule(self.rule) rule = self.chain.rules[0] match = rule.matches[0] - self.assertEquals(match.dports, '1111,2222') + self.assertEqual(match.dports, '1111,2222') class TestXTUdpMatch(unittest.TestCase): @@ -135,9 +135,9 @@ def test_udp_port(self): "!12345:12346", "0:1234", "! 1234", "!0:12345", "!1234:65535"]: self.match.sport = port - self.assertEquals(self.match.sport, port.replace(" ", "")) + self.assertEqual(self.match.sport, port.replace(" ", "")) self.match.dport = port - self.assertEquals(self.match.dport, port.replace(" ", "")) + self.assertEqual(self.match.dport, port.replace(" ", "")) self.match.reset() for port in ["-1", "asdf", "!asdf"]: try: @@ -188,7 +188,7 @@ def tearDown(self): def test_mark(self): for mark in ["0x7b", "! 0x7b", "0x7b/0xfffefffe", "!0x7b/0xff00ff00"]: self.match.mark = mark - self.assertEquals(self.match.mark, mark.replace(" ", "")) + self.assertEqual(self.match.mark, mark.replace(" ", "")) self.match.reset() for mark in ["0xffffffffff", "123/0xffffffff1", "!asdf", "1234:1233"]: try: @@ -232,7 +232,7 @@ def tearDown(self): def test_limit(self): for limit in ["1/sec", "5/min", "3/hour"]: self.match.limit = limit - self.assertEquals(self.match.limit, limit) + self.assertEqual(self.match.limit, limit) self.match.reset() for limit in ["asdf", "123/1", "!1", "!1/second"]: try: @@ -284,7 +284,7 @@ def tearDown(self): def test_icmpv6(self): self.chain.insert_rule(self.rule) rule = self.chain.rules[0] - self.assertEquals(self.rule, rule) + self.assertEqual(self.rule, rule) class TestCommentMatch(unittest.TestCase): @@ -310,7 +310,7 @@ def test_comment(self): self.match.reset() self.match.comment = comment self.chain.insert_rule(self.rule) - self.assertEquals(self.match.comment, comment) + self.assertEqual(self.match.comment, comment) class TestIprangeMatch(unittest.TestCase): @@ -389,8 +389,10 @@ def test_state(self): self.chain.insert_rule(self.rule) rule = self.chain.rules[0] m = rule.matches[0] - self.assertEquals(m.name, "state") - self.assertEquals(m.state, "RELATED,ESTABLISHED") + self.assertEqual(m.name, "state") + self.assertEqual(m.state, "RELATED,ESTABLISHED") + self.assertEqual(rule.matches[0].name, self.rule.matches[0].name) + self.assertEqual(rule, self.rule) class TestXTConntrackMatch(unittest.TestCase): @@ -425,7 +427,7 @@ def test_state(self): rule = self.chain.rules[0] m = rule.matches[0] self.assertTrue(m.name, ["conntrack"]) - self.assertEquals(m.ctstate, "NEW,RELATED") + self.assertEqual(m.ctstate, "NEW,RELATED") class TestHashlimitMatch(unittest.TestCase): @@ -464,10 +466,10 @@ def test_hashlimit(self): rule = self.chain.rules[0] m = rule.matches[0] self.assertTrue(m.name, ["hashlimit"]) - self.assertEquals(m.hashlimit_name, "foo") - self.assertEquals(m.hashlimit_mode, "srcip") - self.assertEquals(m.hashlimit_upto, "200/sec") - self.assertEquals(m.hashlimit_burst, "5") + self.assertEqual(m.hashlimit_name, "foo") + self.assertEqual(m.hashlimit_mode, "srcip") + self.assertEqual(m.hashlimit_upto, "200/sec") + self.assertEqual(m.hashlimit_burst, "5") def suite(): From 54c63684f91457dba46095b7f4e4668bdb421e14 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Sun, 19 May 2019 19:00:55 +0000 Subject: [PATCH 097/135] Add support for _missing_ and _goto_ target in iptc.easy module --- iptc/easy.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 22f9743..3eb056c 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -295,6 +295,8 @@ def encode_iptc_rule(rule_d, ipv6=False): # Basic rule attributes rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') iptc_rule = Rule6() if ipv6 else Rule() + # Set default target + rule_d.setdefault('target', '') # Avoid issues with matches that require basic parameters to be configured first for name in rule_attr: if name in rule_d: @@ -347,7 +349,10 @@ def decode_iptc_rule(iptc_rule, ipv6=False): name = iptc_rule.target.name.replace('-', '_') d['target'] = {name:iptc_rule.target.get_all_parameters()} elif iptc_rule.target and iptc_rule.target.name: - d['target'] = iptc_rule.target.name + if iptc_rule.target.goto: + d['target'] = {'goto':iptc_rule.target.name} + else: + d['target'] = iptc_rule.target.name # Return a filtered dictionary return _filter_empty_field(d) @@ -412,10 +417,12 @@ def _iptc_setmatch(iptc_rule, name, value): def _iptc_settarget(iptc_rule, value): # Target is dictionary - Use only 1 pair key/value if isinstance(value, dict): - for k, v in value.items(): - iptc_target = iptc_rule.create_target(k) - _iptc_setattr_d(iptc_target, v) - return + t_name, t_value = next(iter(value.items())) + if t_name == 'goto': + iptc_target = iptc_rule.create_target(t_value, goto=True) + else: + iptc_target = iptc_rule.create_target(t_name) + _iptc_setattr_d(iptc_target, t_value) # Simple target else: iptc_target = iptc_rule.create_target(value) From 57a7cc5f12f3941e7aafef0aed3025892ad8acf6 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Sun, 19 May 2019 19:09:20 +0000 Subject: [PATCH 098/135] Add documentation for goto rules with iptc.easy --- README.md | 7 ++++++- doc/examples.rst | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 31d2a3a..8d391aa 100755 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ High level abstractions ``python-iptables`` implements a low-level interface that tries to closely match the underlying C libraries. The module ``iptc.easy`` improves the usability of the library by providing a rich set of high-level functions -designed to simplify the interaction with the library, for example:: +designed to simplify the interaction with the library, for example: >>> import iptc >>> iptc.easy.dump_table('nat', ipv6=False) @@ -160,6 +160,11 @@ designed to simplify the interaction with the library, for example:: [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}] >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True) + >>> # Example of goto rule // iptables -A FORWARD -p gre -g TestChainGoto + >>> iptc.easy.add_chain('filter', 'TestChainGoto') + >>> rule_goto_d = {'protocol': 'gre', 'target': {'goto': 'TestChainGoto'}} + >>> iptc.easy.insert_rule('filter', 'FORWARD', rule_goto_d) + Rules ----- diff --git a/doc/examples.rst b/doc/examples.rst index 2f6d9a4..b489e08 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -7,7 +7,7 @@ High level abstractions ``python-iptables`` implements a low-level interface that tries to closely match the underlying C libraries. The module ``iptc.easy`` improves the usability of the library by providing a rich set of high-level functions -designed to simplify the interaction with the library, for example:: +designed to simplify the interaction with the library, for example: >>> import iptc >>> iptc.easy.dump_table('nat', ipv6=False) @@ -26,6 +26,11 @@ designed to simplify the interaction with the library, for example:: [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}] >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True) + >>> # Example of goto rule // iptables -A FORWARD -p gre -g TestChainGoto + >>> iptc.easy.add_chain('filter', 'TestChainGoto') + >>> rule_goto_d = {'protocol': 'gre', 'target': {'goto': 'TestChainGoto'}} + >>> iptc.easy.insert_rule('filter', 'FORWARD', rule_goto_d) + Rules ----- From 9f348f0fa93f4b4a00104e825241be25674019fc Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 22 May 2019 15:15:26 -0700 Subject: [PATCH 099/135] Update list of Python test envs on Travis --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 23e2a11..e7f9a8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,11 @@ +dist: xenial language: python python: - - "2.6" - "2.7" - "3.4" + - "3.5" + - "3.6" + - "3.7" install: - python setup.py build - python setup.py install From eb5915a9038b2009a147c34ced191baecd440245 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Sun, 19 May 2019 19:37:13 +0000 Subject: [PATCH 100/135] Apply subnet mask to IP address --- iptc/ip4tc.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 29d77ea..b48eaa6 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1064,9 +1064,6 @@ def set_src(self, src): saddr = _a_to_i(socket.inet_pton(socket.AF_INET, addr)) except socket.error: raise ValueError("invalid address %s" % (addr)) - ina = in_addr() - ina.s_addr = ct.c_uint32(saddr) - self.entry.ip.src = ina if not netm.isdigit(): try: @@ -1080,8 +1077,11 @@ def set_src(self, src): nmask = socket.htonl((2 ** imask - 1) << (32 - imask)) neta = in_addr() neta.s_addr = ct.c_uint32(nmask) - self.entry.ip.smsk = neta + # Apply subnet mask to IP address + ina = in_addr() + ina.s_addr = ct.c_uint32(saddr & nmask) + self.entry.ip.src = ina src = property(get_src, set_src) """This is the source network address with an optional network mask in @@ -1125,9 +1125,6 @@ def set_dst(self, dst): daddr = _a_to_i(socket.inet_pton(socket.AF_INET, addr)) except socket.error: raise ValueError("invalid address %s" % (addr)) - ina = in_addr() - ina.s_addr = ct.c_uint32(daddr) - self.entry.ip.dst = ina if not netm.isdigit(): try: @@ -1142,6 +1139,10 @@ def set_dst(self, dst): neta = in_addr() neta.s_addr = ct.c_uint32(nmask) self.entry.ip.dmsk = neta + # Apply subnet mask to IP address + ina = in_addr() + ina.s_addr = ct.c_uint32(daddr & nmask) + self.entry.ip.dst = ina dst = property(get_dst, set_dst) """This is the destination network address with an optional network mask From d788d71fc74765bf01a92a3f990e79a65cce4b30 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Wed, 22 May 2019 18:40:44 +0000 Subject: [PATCH 101/135] Fix test_rule_address to compare against valid addresses --- tests/test_iptc.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_iptc.py b/tests/test_iptc.py index 519e228..1d9f775 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -620,13 +620,13 @@ def tearDown(self): def test_rule_address(self): # valid addresses rule = iptc.Rule() - for addr in [("127.0.0.1/255.255.255.0", "127.0.0.1/255.255.255.0"), - ("!127.0.0.1/255.255.255.0", "!127.0.0.1/255.255.255.0"), - ("127.0.0.1/255.255.128.0", "127.0.0.1/255.255.128.0"), - ("127.0.0.1/16", "127.0.0.1/255.255.0.0"), - ("127.0.0.1/24", "127.0.0.1/255.255.255.0"), - ("127.0.0.1/17", "127.0.0.1/255.255.128.0"), - ("!127.0.0.1/17", "!127.0.0.1/255.255.128.0")]: + for addr in [("127.0.0.1/255.255.255.0", "127.0.0.0/255.255.255.0"), + ("!127.0.0.1/255.255.255.0", "!127.0.0.0/255.255.255.0"), + ("127.0.0.1/255.255.128.0", "127.0.0.0/255.255.128.0"), + ("127.0.0.1/16", "127.0.0.0/255.255.0.0"), + ("127.0.0.1/24", "127.0.0.0/255.255.255.0"), + ("127.0.0.1/17", "127.0.0.0/255.255.128.0"), + ("!127.0.0.1/17", "!127.0.0.0/255.255.128.0")]: rule.src = addr[0] self.assertEquals(rule.src, addr[1]) rule.dst = addr[0] From ee827b3fc7a9b783a50682ae149c64b0347cbc02 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Sun, 19 May 2019 19:00:55 +0000 Subject: [PATCH 102/135] Add support for _missing_ and _goto_ target in iptc.easy module --- iptc/easy.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 22f9743..3eb056c 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -295,6 +295,8 @@ def encode_iptc_rule(rule_d, ipv6=False): # Basic rule attributes rule_attr = ('src', 'dst', 'protocol', 'in-interface', 'out-interface', 'fragment') iptc_rule = Rule6() if ipv6 else Rule() + # Set default target + rule_d.setdefault('target', '') # Avoid issues with matches that require basic parameters to be configured first for name in rule_attr: if name in rule_d: @@ -347,7 +349,10 @@ def decode_iptc_rule(iptc_rule, ipv6=False): name = iptc_rule.target.name.replace('-', '_') d['target'] = {name:iptc_rule.target.get_all_parameters()} elif iptc_rule.target and iptc_rule.target.name: - d['target'] = iptc_rule.target.name + if iptc_rule.target.goto: + d['target'] = {'goto':iptc_rule.target.name} + else: + d['target'] = iptc_rule.target.name # Return a filtered dictionary return _filter_empty_field(d) @@ -412,10 +417,12 @@ def _iptc_setmatch(iptc_rule, name, value): def _iptc_settarget(iptc_rule, value): # Target is dictionary - Use only 1 pair key/value if isinstance(value, dict): - for k, v in value.items(): - iptc_target = iptc_rule.create_target(k) - _iptc_setattr_d(iptc_target, v) - return + t_name, t_value = next(iter(value.items())) + if t_name == 'goto': + iptc_target = iptc_rule.create_target(t_value, goto=True) + else: + iptc_target = iptc_rule.create_target(t_name) + _iptc_setattr_d(iptc_target, t_value) # Simple target else: iptc_target = iptc_rule.create_target(value) From 7aeadfb79d6f96c69c950a86d8a3737f55caa3d0 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Sun, 19 May 2019 19:09:20 +0000 Subject: [PATCH 103/135] Add documentation for goto rules with iptc.easy --- README.md | 7 ++++++- doc/examples.rst | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 31d2a3a..8d391aa 100755 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ High level abstractions ``python-iptables`` implements a low-level interface that tries to closely match the underlying C libraries. The module ``iptc.easy`` improves the usability of the library by providing a rich set of high-level functions -designed to simplify the interaction with the library, for example:: +designed to simplify the interaction with the library, for example: >>> import iptc >>> iptc.easy.dump_table('nat', ipv6=False) @@ -160,6 +160,11 @@ designed to simplify the interaction with the library, for example:: [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}] >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True) + >>> # Example of goto rule // iptables -A FORWARD -p gre -g TestChainGoto + >>> iptc.easy.add_chain('filter', 'TestChainGoto') + >>> rule_goto_d = {'protocol': 'gre', 'target': {'goto': 'TestChainGoto'}} + >>> iptc.easy.insert_rule('filter', 'FORWARD', rule_goto_d) + Rules ----- diff --git a/doc/examples.rst b/doc/examples.rst index 2f6d9a4..b489e08 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -7,7 +7,7 @@ High level abstractions ``python-iptables`` implements a low-level interface that tries to closely match the underlying C libraries. The module ``iptc.easy`` improves the usability of the library by providing a rich set of high-level functions -designed to simplify the interaction with the library, for example:: +designed to simplify the interaction with the library, for example: >>> import iptc >>> iptc.easy.dump_table('nat', ipv6=False) @@ -26,6 +26,11 @@ designed to simplify the interaction with the library, for example:: [{'protocol': 'tcp', 'target': 'ACCEPT', 'tcp': {'dport': '22'}}] >>> iptc.easy.delete_chain('filter', 'TestChain', flush=True) + >>> # Example of goto rule // iptables -A FORWARD -p gre -g TestChainGoto + >>> iptc.easy.add_chain('filter', 'TestChainGoto') + >>> rule_goto_d = {'protocol': 'gre', 'target': {'goto': 'TestChainGoto'}} + >>> iptc.easy.insert_rule('filter', 'FORWARD', rule_goto_d) + Rules ----- From 8f52d820d48070f0756f239760c1d35e4dba168a Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Tue, 4 Jun 2019 15:48:27 -0700 Subject: [PATCH 104/135] Release 0.14.0 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 978a432..4f11b38 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.14.0-dev" +__version__ = "0.14.0" From cc1af53e4e650a8f0da052c9b74377579949269d Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Tue, 4 Jun 2019 15:52:37 -0700 Subject: [PATCH 105/135] Start 0.15.0-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 4f11b38..1ded765 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.14.0" +__version__ = "0.15.0-dev" From 1b83634b19ef5276669ad40af65a85d5e76ba50b Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 25 Jun 2019 22:17:14 +0000 Subject: [PATCH 106/135] Add unittest for failing match recent module --- tests/test_matches.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_matches.py b/tests/test_matches.py index 063cfb4..2093ad2 100755 --- a/tests/test_matches.py +++ b/tests/test_matches.py @@ -471,6 +471,35 @@ def test_hashlimit(self): self.assertEqual(m.hashlimit_upto, "200/sec") self.assertEqual(m.hashlimit_burst, "5") +class TestRecentMatch(unittest.TestCase): + def setUp(self): + self.table = 'filter' + self.chain = 'iptc_test_recent' + iptc.easy.delete_chain(self.table, self.chain, ipv6=False, flush=True, raise_exc=False) + iptc.easy.add_chain(self.table, self.chain, ipv6=False, raise_exc=True) + + def tearDown(self): + iptc.easy.delete_chain(self.table, self.chain, ipv6=False, flush=True, raise_exc=False) + + def test_recent(self): + rule_d = { + 'protocol': 'udp', + 'recent': { + 'mask': '255.255.255.255', + 'update': '', + 'seconds': '60', + 'rsource': '', + 'name': 'UDP-PORTSCAN', + }, + 'target': { + 'REJECT':{ + 'reject-with': 'icmp-port-unreachable' + } + } + } + iptc.easy.add_rule(self.table, self.chain, rule_d) + rule2_d = iptc.easy.get_rule(self.table, self.chain, -1) + self.assertEqual(rule_d, rule2_d) def suite(): suite_match = unittest.TestLoader().loadTestsFromTestCase(TestMatch) @@ -488,6 +517,8 @@ def suite(): TestXTConntrackMatch) suite_hashlimit = unittest.TestLoader().loadTestsFromTestCase( TestHashlimitMatch) + suite_recent = unittest.TestLoader().loadTestsFromTestCase( + TestRecentMatch) extra_suites = [] if is_table6_available(iptc.Table6.FILTER): extra_suites += unittest.TestLoader().loadTestsFromTestCase( @@ -496,7 +527,7 @@ def suite(): return unittest.TestSuite([suite_match, suite_udp, suite_mark, suite_limit, suite_mport, suite_comment, suite_iprange, suite_state, suite_conntrack, - suite_hashlimit] + extra_suites) + suite_hashlimit, suite_recent] + extra_suites) def run_tests(): From 9b1300f34ad46ea3f92139689e0fd25bd97be505 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Santos Date: Tue, 25 Jun 2019 22:32:01 +0000 Subject: [PATCH 107/135] Fix set attribute functions for match, target and rule --- iptc/easy.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/iptc/easy.py b/iptc/easy.py index 3eb056c..7f17a4c 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -300,7 +300,7 @@ def encode_iptc_rule(rule_d, ipv6=False): # Avoid issues with matches that require basic parameters to be configured first for name in rule_attr: if name in rule_d: - _iptc_setrule(iptc_rule, name, rule_d[name]) + setattr(iptc_rule, name.replace('-', '_'), rule_d[name]) for name, value in rule_d.items(): try: if name in rule_attr: @@ -388,18 +388,6 @@ def _iptc_getchain(table, chain, ipv6=False, raise_exc=True): except Exception as e: if raise_exc: raise -def _iptc_setattr(object, name, value): - # Translate attribute name - name = name.replace('-', '_') - setattr(object, name, value) - -def _iptc_setattr_d(object, value_d): - for name, value in value_d.items(): - _iptc_setattr(object, name, value) - -def _iptc_setrule(iptc_rule, name, value): - _iptc_setattr(iptc_rule, name, value) - def _iptc_setmatch(iptc_rule, name, value): # Iterate list/tuple recursively if isinstance(value, list) or isinstance(value, tuple): @@ -408,21 +396,21 @@ def _iptc_setmatch(iptc_rule, name, value): # Assign dictionary value elif isinstance(value, dict): iptc_match = iptc_rule.create_match(name) - _iptc_setattr_d(iptc_match, value) + [iptc_match.set_parameter(k, v) for k, v in value.items()] # Assign value directly else: iptc_match = iptc_rule.create_match(name) - _iptc_setattr(iptc_match, name, value) + iptc_match.set_parameter(name, value) def _iptc_settarget(iptc_rule, value): - # Target is dictionary - Use only 1 pair key/value + # Target is dictionary - Use only 1st pair key/value if isinstance(value, dict): t_name, t_value = next(iter(value.items())) if t_name == 'goto': iptc_target = iptc_rule.create_target(t_value, goto=True) else: iptc_target = iptc_rule.create_target(t_name) - _iptc_setattr_d(iptc_target, t_value) + [iptc_target.set_parameter(k, v) for k, v in t_value.items()] # Simple target else: iptc_target = iptc_rule.create_target(value) From 6bf2cf1b8d34fa1140e4170d940421bec5fe0d6c Mon Sep 17 00:00:00 2001 From: Fabian Dellwing Date: Tue, 8 Oct 2019 08:14:10 +0200 Subject: [PATCH 108/135] use absolute path for ldconfig --- iptc/xtables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index 179649d..fafb6af 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -808,7 +808,7 @@ class xtables_target(ct.Union): ldconfig_path_regex = re.compile('^(/.*):$') import subprocess ldconfig = subprocess.Popen( - ('ldconfig', '-N', '-v'), + ('/sbin/ldconfig', '-N', '-v'), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True ) ldconfig_out, ldconfig_err = ldconfig.communicate() From c4dced49810299a781d7eb41e25a5b8a10e4c8d7 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 16 Oct 2019 11:12:44 -0700 Subject: [PATCH 109/135] This fixes #271 --- iptc/ip4tc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 7080d50..86c1c8b 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -419,6 +419,7 @@ def get_all_parameters(self): res = shlex.split(buf) res.reverse() inv = False + key = None while len(res) > 0: x = res.pop() if x == '!': @@ -433,7 +434,11 @@ def get_all_parameters(self): params[key] = [] inv = False continue - params[key].append(x) # This is a parameter value. + # At this point key should be set, unless the output from save is + # not formatted right. Let's be defensive, since some users + # reported that problem. + if key is not None: + params[key].append(x) # This is a parameter value. return params def _update_parameters(self): From 7fe9a9982bb9ddb0fa09cb12a0bc9f3d1001b382 Mon Sep 17 00:00:00 2001 From: Jesus Llorente Date: Mon, 28 Oct 2019 22:29:35 +0100 Subject: [PATCH 110/135] Extended rule counters capabilities (#289) * Extended rule counters capabilities * Remove counters in tests when comparing rules --- README.md | 1 + doc/examples.rst | 1 + iptc/easy.py | 8 ++++++++ iptc/ip4tc.py | 9 +++++++++ tests/test_iptc.py | 4 ++++ tests/test_matches.py | 2 ++ 6 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 8d391aa..bd966b4 100755 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ designed to simplify the interaction with the library, for example: {'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []} >>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False) [{'comment': {'comment': 'DNS traffic to Google'}, + 'counters': (1, 56), 'dst': '8.8.8.8/32', 'protocol': 'udp', 'target': 'ACCEPT', diff --git a/doc/examples.rst b/doc/examples.rst index b489e08..98c6378 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -14,6 +14,7 @@ designed to simplify the interaction with the library, for example: {'INPUT': [], 'OUTPUT': [], 'POSTROUTING': [], 'PREROUTING': []} >>> iptc.easy.dump_chain('filter', 'OUTPUT', ipv6=False) [{'comment': {'comment': 'DNS traffic to Google'}, + 'counters': (1, 56), 'dst': '8.8.8.8/32', 'protocol': 'udp', 'target': 'ACCEPT', diff --git a/iptc/easy.py b/iptc/easy.py index 7f17a4c..d4df85e 100644 --- a/iptc/easy.py +++ b/iptc/easy.py @@ -305,6 +305,8 @@ def encode_iptc_rule(rule_d, ipv6=False): try: if name in rule_attr: continue + elif name == 'counters': + _iptc_setcounters(iptc_rule, value) elif name == 'target': _iptc_settarget(iptc_rule, value) else: @@ -353,6 +355,8 @@ def decode_iptc_rule(iptc_rule, ipv6=False): d['target'] = {'goto':iptc_rule.target.name} else: d['target'] = iptc_rule.target.name + # Get counters + d['counters'] = iptc_rule.counters # Return a filtered dictionary return _filter_empty_field(d) @@ -388,6 +392,10 @@ def _iptc_getchain(table, chain, ipv6=False, raise_exc=True): except Exception as e: if raise_exc: raise +def _iptc_setcounters(iptc_rule, value): + # Value is a tuple (numberOfBytes, numberOfPackets) + iptc_rule.counters = value + def _iptc_setmatch(iptc_rule, name, value): # Iterate list/tuple recursively if isinstance(value, list) or isinstance(value, tuple): diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 86c1c8b..4c5d690 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -1286,6 +1286,15 @@ def get_counters(self): counters = self.entry.counters return counters.pcnt, counters.bcnt + def set_counters(self, counters): + """This method set a tuple pair of the packet and byte counters of + the rule.""" + self.entry.counters.pcnt = counters[0] + self.entry.counters.bcnt = counters[1] + + counters = property(get_counters, set_counters) + """This is the packet and byte counters of the rule.""" + # override the following three for the IPv6 subclass def _entry_size(self): return xt_align(ct.sizeof(ipt_entry)) diff --git a/tests/test_iptc.py b/tests/test_iptc.py index 1d9f775..d630c87 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -581,6 +581,8 @@ def test_rule_to_dict(self): target = iptc.Target(rule, "ACCEPT") rule.target = target rule_d = iptc.easy.decode_iptc_rule(rule, ipv6=True) + # Remove counters when comparing rules + rule_d.pop('counters', None) self.assertEqual(rule_d, {"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}) def test_rule_from_dict(self): @@ -939,6 +941,8 @@ def test_rule_to_dict(self): target = iptc.Target(rule, "ACCEPT") rule.target = target rule_d = iptc.easy.decode_iptc_rule(rule) + # Remove counters when comparing rules + rule_d.pop('counters', None) self.assertEqual(rule_d, {"protocol": "tcp", "src": "127.0.0.1/32", "target": "ACCEPT"}) def test_rule_from_dict(self): diff --git a/tests/test_matches.py b/tests/test_matches.py index 2093ad2..d7c1995 100755 --- a/tests/test_matches.py +++ b/tests/test_matches.py @@ -499,6 +499,8 @@ def test_recent(self): } iptc.easy.add_rule(self.table, self.chain, rule_d) rule2_d = iptc.easy.get_rule(self.table, self.chain, -1) + # Remove counters when comparing rules + rule2_d.pop('counters', None) self.assertEqual(rule_d, rule2_d) def suite(): From 282c790738a111b1ddc27b43ecb0acfab8b09024 Mon Sep 17 00:00:00 2001 From: Chris Heath Date: Mon, 17 Feb 2020 17:01:23 -0500 Subject: [PATCH 111/135] Don't set xtables_{matches,targets} to NULL. Protocol-independent extensions will be looked up once for each protocol and added to the protocol-specific cache. Fixes #282. --- iptc/xtables.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index fafb6af..93bc080 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -1020,10 +1020,6 @@ def find_match(self, name): if ext is not None: return ext - xtables._xtables_matches.value = ct.c_void_p(None).value - if xtables._xtables_pending_matches: - xtables._xtables_pending_matches.value = ct.c_void_p(None).value - match = xtables._xtables_find_match(name, XTF_TRY_LOAD, None) if not match: self._try_register(name) @@ -1045,10 +1041,6 @@ def find_target(self, name): if ext is not None: return ext - xtables._xtables_targets.value = ct.c_void_p(None).value - if xtables._xtables_pending_targets: - xtables._xtables_pending_targets.value = ct.c_void_p(None).value - target = xtables._xtables_find_target(name, XTF_TRY_LOAD) if not target: self._try_register(name) From e3557528d7cdcdc2c579212be8837bc9b54635a4 Mon Sep 17 00:00:00 2001 From: Frank Vanbever Date: Thu, 20 Feb 2020 12:14:08 +0100 Subject: [PATCH 112/135] Add separate mechanism to load libc ctypes.util.find_library() always returns None for systems which do not have the tools installed to determine the location of a given shared library (i.e. ldconfig, gcc, objdump). If find_libary() fails attempt to load known libc by SONAME. Signed-off-by: Frank Vanbever --- iptc/ip4tc.py | 4 ++-- iptc/util.py | 16 ++++++++++++++++ iptc/xtables.py | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 4c5d690..4ddd2dc 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -9,7 +9,7 @@ import struct import weakref -from .util import find_library, load_kernel +from .util import find_library, load_kernel, find_libc from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables, xt_align, xt_counters, xt_entry_target, xt_entry_match) @@ -26,7 +26,7 @@ _IFNAMSIZ = 16 -_libc = ct.CDLL("libc.so.6") +_libc = find_libc() _get_errno_loc = _libc.__errno_location _get_errno_loc.restype = ct.POINTER(ct.c_int) _malloc = _libc.malloc diff --git a/iptc/util.py b/iptc/util.py index ae5fb9b..e6b1649 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -109,3 +109,19 @@ def find_library(*names): major = int(m.group(1)) return lib, major return None, None + + +def find_libc(): + lib = ctypes.util.find_library('c') + if lib is not None: + return ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL) + + libnames = ['libc.so.6', 'libc.so.0', 'libc.so'] + for name in libnames: + try: + lib = ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL) + return lib + except: + pass + + return None diff --git a/iptc/xtables.py b/iptc/xtables.py index 93bc080..cf21029 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -6,7 +6,7 @@ import weakref from . import version -from .util import find_library +from .util import find_library, find_libc from .errors import * XT_INV_PROTO = 0x40 # invert the sense of PROTO @@ -792,7 +792,7 @@ class xtables_target(ct.Union): ("v12", _xtables_target_v12)] -_libc, _ = find_library("c") +_libc = find_libc() _optind = ct.c_long.in_dll(_libc, "optind") _optarg = ct.c_char_p.in_dll(_libc, "optarg") From 899d25c511c6ce779b7153e9ae2e41055b30b9c5 Mon Sep 17 00:00:00 2001 From: Frank Vanbever Date: Mon, 9 Mar 2020 12:36:47 +0100 Subject: [PATCH 113/135] Add '.so' as additional shared object suffix EXT_SUFFIX includes a platform information tag starting from Python 3.5 [0] For example: >>> sysconfig.get_config_var("EXT_SUFFIX") '.cpython-38-aarch64-linux-gnu.so' This suffix only applies to cpython extensions i.e. not to the iptables shared objects. Adding '.so' as an additional suffix for shared objects fixes the issue. Fixes: Issue #301 Signed-off-by: Frank Vanbever [0]: https://docs.python.org/3/whatsnew/3.5.html#build-and-c-api-changes --- iptc/util.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/iptc/util.py b/iptc/util.py index e6b1649..04fe905 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -80,12 +80,19 @@ def _do_find_library(name): def _find_library(*names): + exts = [] if version_info >= (3, 3): - ext = get_config_var("EXT_SUFFIX") + exts.append(get_config_var("EXT_SUFFIX")) else: - ext = get_config_var('SO') + exts.append(get_config_var('SO')) + + if version_info >= (3, 5): + exts.append('.so') + for name in names: - libnames = [name, "lib" + name, name + ext, "lib" + name + ext] + libnames = [name, "lib" + name] + for ext in exts: + libnames += [name + ext, "lib" + name + ext] libdir = os.environ.get('IPTABLES_LIBDIR', None) if libdir is not None: libdirs = libdir.split(':') From 30cd7ce19d0eb8e8d96ad3b03a9073e80b98bdd0 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 14 Jun 2020 14:10:22 -0700 Subject: [PATCH 114/135] Release v1.0.0 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 1ded765..2657151 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "0.15.0-dev" +__version__ = "1.0.0" From 34e5cf653aa107a0a2279936dd3f6850bb3ed0bb Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 14 Jun 2020 14:34:50 -0700 Subject: [PATCH 115/135] Start v1.0.1-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 2657151..13c18f6 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "1.0.0" +__version__ = "1.0.1-dev" From 542efdb739b4e3ef6f28274d23b506bf0027eec2 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 14 Jun 2020 14:39:01 -0700 Subject: [PATCH 116/135] Fix README.md permissions --- README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 README.md diff --git a/README.md b/README.md old mode 100755 new mode 100644 From 8e68d16969632d629fa42a1fafc0bf2edc84e678 Mon Sep 17 00:00:00 2001 From: AZaugg Date: Thu, 13 Oct 2022 10:20:46 -0700 Subject: [PATCH 117/135] Changed regex used to ind xtables libs Updated the regular expression for ldconfig versions packaged in glibc2.28 and greater Closes #329 --- iptc/xtables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/xtables.py b/iptc/xtables.py index cf21029..024779c 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -805,7 +805,7 @@ class xtables_target(ct.Union): _xtables_libdir = os.getenv("XTABLES_LIBDIR") if _xtables_libdir is None: import re - ldconfig_path_regex = re.compile('^(/.*):$') + ldconfig_path_regex = re.compile('^(/.*):($| \(.*\)$)') import subprocess ldconfig = subprocess.Popen( ('/sbin/ldconfig', '-N', '-v'), From f51bd140a7864533e286aefed0aa0c7316243015 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 21 Jan 2023 18:42:37 -0800 Subject: [PATCH 118/135] Add GHA workflow --- .github/workflows/build.yaml | 32 ++++++++++++++++++++++++++++++++ .travis.yml | 16 ---------------- 2 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/build.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..bedecd5 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,32 @@ +name: Build & Test +on: + push: + branches: + - main + pull_request: +jobs: + build-and-test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: + - "3.7" + - "3.8" + - "3.9" + - "3.10" + - "3.11" + steps: + - name: Check out code + uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Build package + run: python setup.py build + - name: Install package + run: python setup.py install + - name: Install coveralls + run: sudo pip install coveralls + - name: Run tests + run: sudo PATH=$PATH coverage run setup.py test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e7f9a8c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -dist: xenial -language: python -python: - - "2.7" - - "3.4" - - "3.5" - - "3.6" - - "3.7" -install: - - python setup.py build - - python setup.py install - - sudo pip install coveralls -script: - - sudo PATH=$PATH coverage run setup.py test -after_success: - coveralls From 479b09d765988dfa52503f23bd035eb30ea6ee9c Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sat, 21 Jan 2023 16:36:06 -0800 Subject: [PATCH 119/135] Release v1.0.1 --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 13c18f6..80ed889 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "1.0.1-dev" +__version__ = "1.0.1" From bea578318f596c94a8a263867985130068e1fd10 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 10:58:05 -0800 Subject: [PATCH 120/135] Update languages in setup.py --- setup.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/setup.py b/setup.py index 145f0c3..efefa07 100644 --- a/setup.py +++ b/setup.py @@ -38,10 +38,7 @@ "Topic :: System :: Networking :: Firewalls", "Topic :: System :: Systems Administration", "Programming Language :: Python :: 2", - "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.3", - "Programming Language :: Python :: 3.4", "Programming Language :: Python :: Implementation :: CPython", ], license="Apache License, Version 2.0", From 9bc14a02c1e6d21aa5f4cf1757154c22b5644857 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 11:39:49 -0800 Subject: [PATCH 121/135] Automate releases --- .github/workflows/build.yaml | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index bedecd5..c3b8396 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,6 +3,8 @@ on: push: branches: - main + tags: + - "v*" pull_request: jobs: build-and-test: @@ -23,10 +25,38 @@ jobs: with: python-version: ${{ matrix.python-version }} - name: Build package - run: python setup.py build - - name: Install package - run: python setup.py install + run: | + python -m pip install --upgrade build twine + python -m build + twine check --strict dist/* - name: Install coveralls run: sudo pip install coveralls - name: Run tests run: sudo PATH=$PATH coverage run setup.py test + + release: + runs-on: ubuntu-latest + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + needs: + - build-and-test (3.7) + - build-and-test (3.8) + - build-and-test (3.9) + - build-and-test (3.10) + - build-and-test (3.11) + steps: + - name: Check out code + uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + - name: Build package + run: | + python -m pip install --upgrade build twine + python -m build + twine check --strict dist/* + - name: Publish package + uses: pypa/gh-action-pypi-publish@release/v1 + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} From 5ea087f886bd74e85664d97fc4b4b79d833dc840 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 11:40:01 -0800 Subject: [PATCH 122/135] Add missing fields to setup.py --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index efefa07..c3e4a46 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,8 @@ name=__pkgname__, version=__version__, description="Python bindings for iptables", + long_description="Python bindings for classic iptables", + long_description_content_type="text/x-rst", author="Vilmos Nebehaj", author_email="v.nebehaj@gmail.com", url="https://github.com/ldx/python-iptables", From d6f0faf52f2267510996db9575106a83ac34c287 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 11:42:07 -0800 Subject: [PATCH 123/135] Fix dependency --- .github/workflows/build.yaml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index c3b8396..b530f88 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -38,11 +38,7 @@ jobs: runs-on: ubuntu-latest if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') needs: - - build-and-test (3.7) - - build-and-test (3.8) - - build-and-test (3.9) - - build-and-test (3.10) - - build-and-test (3.11) + - build-and-test steps: - name: Check out code uses: actions/checkout@v2 From 158a82a31b95d6421c12e4ae9328febd8ed7ffb9 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 11:48:15 -0800 Subject: [PATCH 124/135] Upload release to GitHub --- .github/workflows/build.yaml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b530f88..2efe8e8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -56,3 +56,28 @@ jobs: with: user: __token__ password: ${{ secrets.PYPI_API_TOKEN }} + - name: Create GitHub release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: ${{ github.ref }} + draft: false + prerelease: false + - name: Set asset name + run: | + export PKG=$(ls dist/ | grep tar) + set -- $PKG + echo "name=$1" >> $GITHUB_ENV + - name: Upload release asset to GitHub + id: upload-release-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: dist/${{ env.name }} + asset_name: ${{ env.name }} + asset_content_type: application/zip From bb2b43b77016bb09bf1f11524a0779bdbdbe5a8e Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 11:53:12 -0800 Subject: [PATCH 125/135] Fix indentation --- .github/workflows/build.yaml | 50 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 2efe8e8..2003385 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -56,28 +56,28 @@ jobs: with: user: __token__ password: ${{ secrets.PYPI_API_TOKEN }} - - name: Create GitHub release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} - draft: false - prerelease: false - - name: Set asset name - run: | - export PKG=$(ls dist/ | grep tar) - set -- $PKG - echo "name=$1" >> $GITHUB_ENV - - name: Upload release asset to GitHub - id: upload-release-asset - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: dist/${{ env.name }} - asset_name: ${{ env.name }} - asset_content_type: application/zip + - name: Create GitHub release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: ${{ github.ref }} + draft: false + prerelease: false + - name: Set asset name + run: | + export PKG=$(ls dist/ | grep tar) + set -- $PKG + echo "name=$1" >> $GITHUB_ENV + - name: Upload release asset to GitHub + id: upload-release-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: dist/${{ env.name }} + asset_name: ${{ env.name }} + asset_content_type: application/zip From 3b0d79c6bf7f54cca36f1ddd39d3374f8542d223 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 12:50:31 -0800 Subject: [PATCH 126/135] Fix upload --- .github/workflows/build.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 2003385..561a473 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -51,6 +51,7 @@ jobs: python -m pip install --upgrade build twine python -m build twine check --strict dist/* + rm -f dist/*.whl - name: Publish package uses: pypa/gh-action-pypi-publish@release/v1 with: From 2cd09a36f3db37d4e5fa9112dc2e2092a0560966 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 22 Jan 2023 12:57:45 -0800 Subject: [PATCH 127/135] Start v1.0.2-dev --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index 80ed889..8a2f162 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "1.0.1" +__version__ = "1.0.2-dev" From af932f847f411f9765034a34e2ec33e839f3df61 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 19 Feb 2025 09:11:02 +0530 Subject: [PATCH 128/135] Trigger build --- .github/workflows/build.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 561a473..fe95d83 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,6 +6,7 @@ on: tags: - "v*" pull_request: + jobs: build-and-test: runs-on: ubuntu-latest From eb0fb688527cce2db0222552f1a6ae9dbd6da5e0 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 19 Feb 2025 09:14:10 +0530 Subject: [PATCH 129/135] Update actions --- .github/workflows/build.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index fe95d83..3c4fe42 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -20,9 +20,9 @@ jobs: - "3.11" steps: - name: Check out code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Build package @@ -42,9 +42,9 @@ jobs: - build-and-test steps: - name: Check out code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: "3.x" - name: Build package From 4fffc0ee73273e70dff138fd76c767ff6b5d24a1 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Wed, 19 Feb 2025 09:17:30 +0530 Subject: [PATCH 130/135] Use earlier image --- .github/workflows/build.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3c4fe42..efc2167 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -9,7 +9,7 @@ on: jobs: build-and-test: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: matrix: python-version: @@ -36,7 +36,7 @@ jobs: run: sudo PATH=$PATH coverage run setup.py test release: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') needs: - build-and-test From d9a70d478d118ddef8cc3ff84c4febc9a0e4e884 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Thu, 20 Feb 2025 03:49:04 +0000 Subject: [PATCH 131/135] Port to Sphinx 8.0 (#349) The old `intersphinx_mapping` format has been removed; it must now map identifiers to (target, inventory) tuples. --- doc/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/conf.py b/doc/conf.py index 6abeedf..ea780c0 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -196,6 +196,6 @@ # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {'http://docs.python.org/': None} +intersphinx_mapping = {'python': ('http://docs.python.org/', None)} autoclass_content="both" From 794aace1fc7b38756ba53c83e4c19a96f81c3763 Mon Sep 17 00:00:00 2001 From: Jeremiah Millay <5504449+floatingstatic@users.noreply.github.com> Date: Wed, 19 Feb 2025 22:51:09 -0500 Subject: [PATCH 132/135] Make python-iptables compatible with python 3.12 (#351) --- iptc/util.py | 7 ++++++- iptc/version.py | 2 +- setup.py | 1 - 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/iptc/util.py b/iptc/util.py index 04fe905..6c1592c 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -3,7 +3,6 @@ import sys import ctypes import ctypes.util -from distutils.sysconfig import get_python_lib from itertools import product from subprocess import Popen, PIPE from sys import version_info @@ -14,6 +13,12 @@ def get_config_var(name): if name == 'SO': return '.so' raise Exception('Not implemented') +try: + from distutils.sysconfig import get_python_lib +except ModuleNotFoundError: + import sysconfig + def get_python_lib(): + return sysconfig.get_path("purelib") def _insert_ko(modprobe, modname): diff --git a/iptc/version.py b/iptc/version.py index 8a2f162..e67fbc7 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "1.0.2-dev" +__version__ = "1.1.0" diff --git a/setup.py b/setup.py index c3e4a46..1929f93 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,6 @@ """python-iptables setup script""" from setuptools import setup, Extension -#from distutils.core import setup, Extension # make pyflakes happy __pkgname__ = None From 9c9b4b7b6b042e475f1c58f8bd2e939dfea3b410 Mon Sep 17 00:00:00 2001 From: Or Yaacov <48175064+oryaacov@users.noreply.github.com> Date: Tue, 4 Mar 2025 09:59:22 -0800 Subject: [PATCH 133/135] fix(ip6tc): ipv6 interface name with 15 chars is too big (#348) * fix(ip6tc): ipv6 interface name with 15 chars is too big * feat(tests): add case for max valid length network interface name --- iptc/ip6tc.py | 55 ++++++++++++++++++++-------------------------- tests/test_iptc.py | 4 +++- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index ed10e17..ea4a1d7 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -412,22 +412,19 @@ def set_dst(self, dst): def get_in_interface(self): intf = "" if self.entry.ipv6.invflags & ip6t_ip6.IP6T_INV_VIA_IN: - intf = "".join(["!", intf]) - iface = bytearray(_IFNAMSIZ) - iface[:len(self.entry.ipv6.iniface)] = self.entry.ipv6.iniface - mask = bytearray(_IFNAMSIZ) - mask[:len(self.entry.ipv6.iniface_mask)] = self.entry.ipv6.iniface_mask - if mask[0] == 0: + intf = "!" + + iface = self.entry.ipv6.iniface.decode() + mask = self.entry.ipv6.iniface_mask + + if len(mask) == 0: return None - for i in range(_IFNAMSIZ): - if mask[i] != 0: - intf = "".join([intf, chr(iface[i])]) - else: - if iface[i - 1] != 0: - intf = "".join([intf, "+"]) - else: - intf = intf[:-1] - break + + intf += iface + if len(iface) == len(mask): + intf += '+' + intf = intf[:_IFNAMSIZ] + return intf def set_in_interface(self, intf): @@ -456,23 +453,19 @@ def set_in_interface(self, intf): def get_out_interface(self): intf = "" if self.entry.ipv6.invflags & ip6t_ip6.IP6T_INV_VIA_OUT: - intf = "".join(["!", intf]) - iface = bytearray(_IFNAMSIZ) - iface[:len(self.entry.ipv6.outiface)] = self.entry.ipv6.outiface - mask = bytearray(_IFNAMSIZ) - mask[:len(self.entry.ipv6.outiface_mask)] = \ - self.entry.ipv6.outiface_mask - if mask[0] == 0: + intf = "!" + + iface = self.entry.ipv6.outiface.decode() + mask = self.entry.ipv6.outiface_mask + + if len(mask) == 0: return None - for i in range(_IFNAMSIZ): - if mask[i] != 0: - intf = "".join([intf, chr(iface[i])]) - else: - if iface[i - 1] != 0: - intf = "".join([intf, "+"]) - else: - intf = intf[:-1] - break + + intf += iface + if len(iface) == len(mask): + intf += '+' + intf = intf[:_IFNAMSIZ] + return intf def set_out_interface(self, intf): diff --git a/tests/test_iptc.py b/tests/test_iptc.py index d630c87..816df86 100755 --- a/tests/test_iptc.py +++ b/tests/test_iptc.py @@ -423,7 +423,9 @@ def test_rule_address(self): def test_rule_interface(self): # valid interfaces rule = iptc.Rule6() - for intf in ["eth0", "eth+", "ip6tnl1", "ip6tnl+", "!ppp0", "!ppp+"]: + + max_length_valid_interface_name = "0123456789abcde" + for intf in ["eth0", "eth+", "ip6tnl1", "ip6tnl+", "!ppp0", "!ppp+", max_length_valid_interface_name]: rule.in_interface = intf self.assertEquals(intf, rule.in_interface) rule.out_interface = intf From c2d0e67abeef7dceec0a1891566dcde79889e048 Mon Sep 17 00:00:00 2001 From: jkklemm Date: Sun, 6 Apr 2025 23:26:56 +0200 Subject: [PATCH 134/135] Speed up code for arm architecture when creating new rules (#341) * speed up code for arm architecture when creating new rules * moved dict to static class variable --- iptc/ip4tc.py | 13 +++++++++++++ iptc/ip6tc.py | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 4ddd2dc..90f5ee7 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -42,10 +42,14 @@ def is_table_available(name): try: + if name in Table.existing_table_names: + return Table.existing_table_names[name] Table(name) + Table.existing_table_names[name] = True return True except IPTCError: pass + Table.existing_table_names[name] = False return False @@ -669,6 +673,11 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string i.e. "" should be used. """ + + STANDARD_TARGETS = ["", "ACCEPT", "DROP", "REJECT", "RETURN", "REDIRECT", "SNAT", "DNAT", \ + "MASQUERADE", "MIRROR", "TOS", "MARK", "QUEUE", "LOG"] + """This is the constant for all standard targets.""" + def __init__(self, rule, name=None, target=None, revision=None, goto=None): """ *rule* is the Rule object this match belongs to; it can be changed @@ -784,6 +793,8 @@ def _create_buffer(self, target): self.reset() def _is_standard_target(self): + if self._name in Target.STANDARD_TARGETS: + return False for t in self._rule.tables: if t.is_chain(self._name): return True @@ -1572,6 +1583,8 @@ class Table(object): """This is the constant for all tables.""" _cache = dict() + existing_table_names = dict() + """Dictionary to check faster if a table is available.""" def __new__(cls, name, autocommit=None): obj = Table._cache.get(name, None) diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index ea4a1d7..e0d8787 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -19,10 +19,14 @@ def is_table6_available(name): try: + if name in Table6.existing_table_names: + return Table6.existing_table_names[name] Table6(name) + Table6.existing_table_names[name] = True return True except IPTCError: pass + Table6.existing_table_names[name] = False return False @@ -572,6 +576,8 @@ class Table6(Table): """This is the constant for all tables.""" _cache = dict() + existing_table_names = dict() + """Dictionary to check faster if a table is available.""" def __new__(cls, name, autocommit=None): obj = Table6._cache.get(name, None) From 2703efb3063f4b62282722f85a1a7f584d9d64c1 Mon Sep 17 00:00:00 2001 From: Vilmos Nebehaj Date: Sun, 6 Apr 2025 15:11:08 -0700 Subject: [PATCH 135/135] Bump version to v1.2.0 (#355) --- iptc/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iptc/version.py b/iptc/version.py index e67fbc7..f6a5157 100644 --- a/iptc/version.py +++ b/iptc/version.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- __pkgname__ = "python-iptables" -__version__ = "1.1.0" +__version__ = "1.2.0"