diff --git a/iptables b/iptables new file mode 160000 index 0000000..3abf5cc --- /dev/null +++ b/iptables @@ -0,0 +1 @@ +Subproject commit 3abf5cc5ac0c32eba2436567d25e175d7e0f42bc diff --git a/iptc/__init__.py b/iptc/__init__.py index 0282bf1..8d3cd8f 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -15,7 +15,7 @@ import weakref import ctypes.util -from xtables import XT_INV_PROTO, NFPROTO_IPV4, XTF_TRY_LOAD, XTablesError, xtables, xtables_globals, xt_align, xt_counters, xt_entry_target, xt_entry_match, _lib_xtwrapper +from xtables import XT_INV_PROTO, NFPROTO_IPV4, XTF_TRY_LOAD, XTablesError, xtables, xtables_globals, xt_align, xt_counters, xt_entry_target, xt_entry_match, _lib_xtwrapper, x6_option_call, x6_option, xtables_target, xtables_match __all__ = ["Table", "Chain", "Rule", "Match", "Target", "Policy", "IPTCError", "POLICY_ACCEPT", "POLICY_DROP", "POLICY_QUEUE", "POLICY_RETURN", @@ -275,6 +275,14 @@ class IPTCError(Exception): _wrap_save = _lib_xtwrapper.wrap_save _wrap_save.restype = ct.c_void_p _wrap_save.argtypes = [ct.c_void_p, ct.POINTER(ipt_ip), ct.c_void_p] +_wrap_x6_parse_match = _lib_xtwrapper.wrap_mpcall +_wrap_x6_parse_match.restype = ct.c_int +_wrap_x6_parse_match.argtables = [ct.c_uint, ct.POINTER(ct.c_char_p), + ct.c_int, ct.POINTER(xtables_match), ct.c_void_p, ct.c_void_p] +_wrap_x6_parse_target = _lib_xtwrapper.wrap_tpcall +_wrap_x6_parse_target.restype = ct.c_int +_wrap_x6_parse_target.argtables = [ct.c_uint, ct.POINTER(ct.c_char_p), + ct.c_int, ct.POINTER(xtables_target), ct.c_void_p, ct.c_void_p] _xt = xtables(NFPROTO_IPV4) @@ -300,31 +308,73 @@ def parse(self, parameter, value): else: inv = ct.c_int(0) - if not self._module.extra_opts: + """if self._module.x6_options: + if self._module.x6_parse == None: + raise NotImplementedError() + else: + for opt in self._module.x6_options: + if opt.name == None: + break + if opt.name == parameter: + cb = x6_option_call() + cb.entry = self._rule.entry and ct.pointer(self._rule.entry) or \ + None + cb.arg = 0 # FIXME + cb.invert = False + cb.ext_name = self._module.name + try: + #cb.data = self._module.t.data + cb.data = None + cb.xflags = self._module.tflags + cb.target = self._module.t + cb.xt_entry = None + self._module.x6_parse(cb) + self._module.tflags = cb.xflags + except AttributeError: + #cb.data = self._module.m.data + cb.data = None + cb.xflags = self._module.mflags + cb.match = self._module.m + cb.xt_entry = None + cb.udata = self._module.udata + self._module.x6_parse(cb) + self._module.mflags = cb.xflags""" + if self._module.extra_opts or self._module.x6_options: + _optarg.value = value + _optind.value = 2 + + argv = (ct.c_char_p * 2)() + argv[0] = parameter + argv[1] = value + + v = self._module.extra_opts or self._module.x6_options + for opt in v: + if opt.name == None: + break + if opt.name == parameter: + entry = self._rule.entry and ct.pointer(self._rule.entry) or \ + None + if self._module.x6_options: + if (isinstance(self, Match)): + rv = _wrap_x6_parse_match(opt.id, argv, inv, ct.pointer(self._module), ct.pointer(self._module.m), entry) + elif (isinstance(self, Target)): + rv = _wrap_x6_parse_target(opt.id, argv, inv, ct.pointer(self._module), ct.pointer(self._module.t), entry) + else: + raise ValueError("invalid calling type") + else: + rv = _wrap_parse(self._module.parse, opt.val, argv, inv, + ct.pointer(self._flags), entry, + ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p))) + if rv != 1: + raise ValueError("invalid value %s" % (value)) + return + elif not opt.name: + break + raise AttributeError("invalid parameter %s" % (parameter)) + else: raise AttributeError("%s: invalid parameter %s" % (self._module.name, parameter)) - _optarg.value = value - _optind.value = 2 - - argv = (ct.c_char_p * 2)() - argv[0] = parameter - argv[1] = value - - for opt in self._module.extra_opts: - if opt.name == parameter: - entry = self._rule.entry and ct.pointer(self._rule.entry) or \ - None - rv = _wrap_parse(self._module.parse, opt.val, argv, inv, - ct.pointer(self._flags), entry, - ct.cast(self._ptrptr, ct.POINTER(ct.c_void_p))) - if rv != 1: - raise ValueError("invalid value %s" % (value)) - return - elif not opt.name: - break - raise AttributeError("invalid parameter %s" % (parameter)) - def final_check(self): if self._module and self._module.final_check: self._module.final_check(self._flags) @@ -494,7 +544,7 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string "" should be used. """ - def __init__(self, rule, name=None, target=None, revision=0): + def __init__(self, rule, name=None, target=None, revision=0, table=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 @@ -508,12 +558,17 @@ def __init__(self, rule, name=None, target=None, revision=0): raise ValueError("can't create target based on nothing") if name == None: name = target.u.user.name + if table == None: + if target == None: + table = TABLE_FILTER + else: + raise ValueError("table must be specified if target provided") self._name = name self._rule = rule self._revision = revision is_standard_target = False - if TABLE_FILTER.is_chain(name): + if table.is_chain(name): is_standard_target = True module = _xt.find_target('standard') else: @@ -1014,7 +1069,7 @@ def _set_rule(self, entry): target = ct.cast(ct.byref(entry, entry.target_offset), ct.POINTER(ipt_entry_target))[0] - self.target = Target(self, target=target) + self.target = Target(self, target=target, table=self.chain.table) jump = self.chain.table.get_target(entry) # standard target is special if jump: self._target.standard_target = jump diff --git a/iptc/xtables.py b/iptc/xtables.py index f9d30ba..423c040 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -73,6 +73,63 @@ class option(ct.Structure): ("flag", ct.POINTER(ct.c_int)), ("val", ct.c_int)] +class x6_option(ct.Structure): + _fields_ = [("name", ct.c_char_p), + ("type", ct.c_uint), + ("id", ct.c_uint), + ("excl", ct.c_uint), + ("also", ct.c_uint), + ("flags", ct.c_uint), + ("ptroff", ct.c_uint), + ("size", ct.c_size_t), + ("min", ct.c_uint), + ("max", ct.c_uint)] + +class x6_option_call_markmask(ct.Structure): + _fields_ = [("mark", ct.c_uint32), + ("mask", ct.c_uint32)] + +class x6_option_call_value(ct.Union): + _fields_ = [("u8", ct.c_uint8), + ("u8_range", ct.c_uint8 * 2), + ("syslog_level", ct.c_uint8), + ("protocol", ct.c_uint8), + ("u16", ct.c_uint16), + ("u16_range", ct.c_uint16 * 2), + ("port", ct.c_uint16), + ("port_range", ct.c_uint16 * 2), + ("u32", ct.c_uint32), + ("u32_range", ct.c_uint32 * 2), + ("u64", ct.c_uint64), + ("u64_range", ct.c_uint64 * 2), + ("dbl", ct.c_double), + # FIXME: Complete other structs. + ("s3", x6_option_call_markmask), + ("ethermac", ct.c_uint8 * 6)] + +class x6_option_call_assoc(ct.Union): + _fields_ = [("match", ct.POINTER(ct.c_void_p)), + ("target", ct.POINTER(ct.c_void_p))] + +class x6_option_call(ct.Structure): + _fields_ = [("arg", ct.c_char_p), + ("ext_name", ct.c_char_p), + ("entry", ct.POINTER(x6_option)), + ("data", ct.c_void_p), + ("xflags", ct.c_uint), + ("invert", ct.c_bool), + ("nvals", ct.c_uint8), + ("val", x6_option_call_value), + ("assoc", x6_option_call_assoc), + ("xt_entry", ct.c_void_p), + ("udata", ct.c_void_p)] + +class x6_option_fcheck(ct.Structure): + _fields_ = [("ext_name", ct.c_char_p), + ("data", ct.c_void_p), + ("udata", ct.c_void_p), + ("xflags", ct.c_uint)] + class xtables_match(ct.Structure): _fields_ = [("version", ct.c_char_p), ("next", ct.c_void_p), @@ -101,6 +158,12 @@ class xtables_match(ct.Structure): ct.POINTER(xt_entry_match))), # Pointer to list of extra command-line options ("extra_opts", ct.POINTER(option)), + # New parser + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(x6_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(x6_option_fcheck))), + ("x6_options", ct.POINTER(x6_option)), + ("udata_size", ct.c_size_t), + ("udata", ct.c_void_p), # Ignore these men behind the curtain: ("option_offset", ct.c_uint), ("m", ct.POINTER(xt_entry_match)), @@ -135,6 +198,12 @@ class xtables_target(ct.Structure): ct.POINTER(xt_entry_target))), # Pointer to list of extra command-line options ("extra_opts", ct.POINTER(option)), + # New parser + ("x6_parse", ct.CFUNCTYPE(None, ct.POINTER(x6_option_call))), + ("x6_fcheck", ct.CFUNCTYPE(None, ct.POINTER(x6_option_fcheck))), + ("x6_options", ct.POINTER(x6_option)), + ("udata_size", ct.c_size_t), + ("udata", ct.c_void_p), # Ignore these men behind the curtain: ("option_offset", ct.c_uint), ("t", ct.POINTER(xt_entry_target)), diff --git a/libxtwrapper/wrapper.c b/libxtwrapper/wrapper.c index e811c24..63d89ee 100644 --- a/libxtwrapper/wrapper.c +++ b/libxtwrapper/wrapper.c @@ -3,6 +3,7 @@ #include #include #include +#include "../iptables/include/xtables.h.in" int kernel_version; @@ -49,3 +50,45 @@ void wrap_save(int (*fn)(), const struct ipt_ip *ip, const void *m) fn(ip, m); fflush(stdout); } + +// FIXME: xt_entry_match/target +// +// For some reason, the m and t fields need to +// be passed into these functions from Python +// and then set back onto the structures that +// originally contained them. I am unsure as +// to why the m->m and t->t values seem to get +// "lost" over the Python <-> native boundary. + +int wrap_mpcall(unsigned int c, char **argv, int inv, + struct xtables_match *m, struct xt_entry_match *mm, void *fw) +{ + int rv = 1; + int err; + + if ((err = setjmp(env)) == 0) { + m->m = mm; // FIXME: See above. + xtables_option_mpcall(c, argv, inv, m, fw); + } else { + errno = err; + } + + return rv; +} + +int wrap_tpcall(unsigned int c, char **argv, int inv, + struct xtables_target *t, struct xt_entry_target *tt, void *fw) +{ + int rv = 1; + int err; + + if ((err = setjmp(env)) == 0) { + t->t = tt; // FIXME: See above. + xtables_option_tpcall(c, argv, inv, t, fw); + } else { + errno = err; + } + + return rv; +} + diff --git a/setup.py b/setup.py old mode 100644 new mode 100755 index 0c5255c..f03547a --- a/setup.py +++ b/setup.py @@ -17,7 +17,12 @@ url = "http://nilvec.com/", packages = ["iptc"], package_dir = {"iptc" : "iptc"}, - ext_modules = [Extension("libxtwrapper", ["libxtwrapper/wrapper.c"])], + ext_modules = [Extension( + "libxtwrapper", + ["libxtwrapper/wrapper.c"], + extra_compile_args = ["-g", "-O0"], + extra_link_args = ["-g", "-O0"] + )], classifiers = [ "Development Status :: 4 - Beta", "Intended Audience :: Developers",