diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..efc2167 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,85 @@ +name: Build & Test +on: + push: + branches: + - main + tags: + - "v*" + pull_request: + +jobs: + build-and-test: + runs-on: ubuntu-20.04 + strategy: + matrix: + python-version: + - "3.7" + - "3.8" + - "3.9" + - "3.10" + - "3.11" + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Build package + 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-20.04 + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + needs: + - build-and-test + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Build package + run: | + 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: + 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 diff --git a/.gitignore b/.gitignore index eeb8a34..ecf313f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,25 @@ /Vagrantfile /.vagrant /tags +*/__pycache__/ +*.pyc +/build/ +/.pybuild/ +*.egg-info + +# Debian build related files +/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 + + +# Added exclusion for PyCharm files +.idea/* diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c3f9c0e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: python -python: - - "2.6" - - "2.7" - - "3.4" -install: - - python setup.py build - - python setup.py install -script: - - echo "y"|sudo PATH=$PATH ./test.py diff --git a/README.md b/README.md index 244dfd1..bd966b4 100644 --- a/README.md +++ b/README.md @@ -34,13 +34,17 @@ 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) -[![Number of Downloads](https://pypip.in/d/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) +[![Coverage Status](https://coveralls.io/repos/ldx/python-iptables/badge.svg?branch=codecoverage)](https://coveralls.io/r/ldx/python-iptables?branch=codecoverage) -[![License](https://pypip.in/license/python-iptables/badge.png)](https://pypi.python.org/pypi/python-iptables) +[![Code Health](https://landscape.io/github/ldx/python-iptables/codecoverage/landscape.svg)](https://landscape.io/github/ldx/python-iptables/codecoverage) + +[![Number of Downloads](https://img.shields.io/pypi/dm/python-iptables.svg)](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 ------------------ @@ -76,7 +80,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 @@ -95,6 +99,29 @@ package can be imported: Of course you need to be root to be able to use iptables. +Using a custom iptables install +------------------------------- + +If you are stuck on a system with an old version of `iptables`, you can +install a more up to date version to a custom location, and ask +`python-iptables` to use libraries at that location. + +To install `iptables` to `/tmp/iptables`: + + % git clone git://git.netfilter.org/iptables && cd iptables + % ./autogen.sh + % ./configure --prefix=/tmp/iptables + % make + % make install + +Make sure the dependencies `iptables` needs are installed. + +Now you can point `python-iptables` to this install path via: + + % sudo PATH=$PATH IPTABLES_LIBDIR=/tmp/iptables/lib XTABLES_LIBDIR=/tmp/iptables/lib/xtables python + >>> import iptc + >>> + What is supported ----------------- @@ -108,6 +135,37 @@ 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'}, + 'counters': (1, 56), + '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) + + >>> # 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 ----- @@ -260,17 +318,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: @@ -519,3 +577,31 @@ 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 +============ + +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](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). + diff --git a/debian/changelog b/debian/changelog index 0d7c90f..e8bab22 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +python-iptables (0.12.0) 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 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; \ 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" diff --git a/doc/examples.rst b/doc/examples.rst index 5efbf42..98c6378 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -1,6 +1,37 @@ 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'}, + 'counters': (1, 56), + '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) + + >>> # 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 ----- @@ -153,19 +184,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 +440,28 @@ 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 +============ + +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 b54b26f..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 @@ -104,6 +112,29 @@ package can be imported:: Of course you need to be root to be able to use iptables. +Using a custom iptables install +------------------------------- + +If you are stuck on a system with an old version of ``iptables``, you can +install a more up to date version to a custom location, and ask +``python-iptables`` to use libraries at that location. + +To install ``iptables`` to ``/tmp/iptables``:: + + % git clone git://git.netfilter.org/iptables && cd iptables + % ./autogen.sh + % ./configure --prefix=/tmp/iptables + % make + % make install + +Make sure the dependencies ``iptables`` needs are installed. + +Now you can point ``python-iptables`` to this install path via:: + + % sudo PATH=$PATH IPTABLES_LIBDIR=/tmp/iptables/lib XTABLES_LIBDIR=/tmp/iptables/lib/xtables python + >>> import iptc + >>> + What is supported ----------------- diff --git a/iptc/__init__.py b/iptc/__init__.py index 9818014..68c19bc 100644 --- a/iptc/__init__.py +++ b/iptc/__init__.py @@ -7,9 +7,10 @@ .. 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 * +import iptc.easy + __all__ = [] diff --git a/iptc/easy.py b/iptc/easy.py new file mode 100644 index 0000000..d4df85e --- /dev/null +++ b/iptc/easy.py @@ -0,0 +1,461 @@ +# -*- coding: utf-8 -*- + +# TODO: +# - Add documentation +# - Add HowToUse examples + +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, raise_exc=True): + """ Flush a table """ + try: + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.flush() + except Exception as e: + if raise_exc: raise + +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 raise_exc: raise + +def zero_all(table, ipv6=False): + """ Zero all tables """ + for table in get_tables(ipv6): + 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, 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 raise_exc: 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, raise_exc=True): + """ Delete a chain """ + try: + if flush: + flush_chain(table, chain, ipv6, raise_exc) + iptc_table = _iptc_gettable(table, ipv6) + iptc_table.delete_chain(chain) + except Exception as e: + if raise_exc: raise + +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 raise_exc: 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, 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: + # 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 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_chain.replace_rule(iptc_new_rule, iptc_chain.rules.index(iptc_old_rule)) + +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)) + 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 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)} + +def dump_table(table, ipv6=False): + """ Return a dictionary representation of a table """ + 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 """ + iptc_chain = _iptc_getchain(table, chain, ipv6) + return [decode_iptc_rule(iptc_rule, ipv6) for iptc_rule in iptc_chain.rules] + + +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, raise_exc=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 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() + # 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: + setattr(iptc_rule, name.replace('-', '_'), rule_d[name]) + for name, value in rule_d.items(): + try: + if name in rule_attr: + continue + elif name == 'counters': + _iptc_setcounters(iptc_rule, value) + 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: + if iptc_rule.target.goto: + 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) + +### 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, raise_exc=True): + """ 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 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): + 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_match.set_parameter(k, v) for k, v in value.items()] + # Assign value directly + else: + iptc_match = iptc_rule.create_match(name) + iptc_match.set_parameter(name, value) + +def _iptc_settarget(iptc_rule, 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_target.set_parameter(k, v) for k, v in t_value.items()] + # Simple target + else: + iptc_target = iptc_rule.create_target(value) + +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 + +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 ### 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/ip4tc.py b/iptc/ip4tc.py index d1931b4..90f5ee7 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) @@ -20,9 +20,13 @@ 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") +_libc = find_libc() _get_errno_loc = _libc.__errno_location _get_errno_loc.restype = ct.POINTER(ct.c_int) _malloc = _libc.malloc @@ -32,13 +36,20 @@ _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: + 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 @@ -95,7 +106,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. """ @@ -217,9 +228,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 @@ -262,6 +273,21 @@ def __init__(self): self._ptrptr = None raise NotImplementedError() + def set_parameter(self, parameter, value=None): + """ + Set a parameter for target or match extension, with an optional value. + + @param parameter: name of the parameter to set + @type parameter: C{str} + + @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: + value = "" + + return self.parse(parameter.replace("_", "-"), value) + def parse(self, parameter, value): # Parameter name must always be a string. parameter = parameter.encode() @@ -397,6 +423,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 == '!': @@ -411,14 +438,26 @@ 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): 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: + 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): @@ -438,7 +477,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.""" @@ -521,37 +561,43 @@ 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: ct.memmove(ct.byref(self._match_buf), ct.byref(match), self.size) self._update_pointers() + self._check_alias() else: self.reset() + def _check_alias(self): + name = self._get_alias_name() + if name is None: + return + 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 - 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]): + 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) - 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) @@ -627,7 +673,12 @@ 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): + + 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 later via *set_rule()*. *name* is the name of the iptables target @@ -637,6 +688,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") @@ -647,10 +699,32 @@ def __init__(self, rule, name=None, target=None, revision=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(ipstruct.flags & _F_GOTO) + if goto is not None: + assert isinstance(goto, bool) + self._goto = goto + if goto: + ipstruct.flags |= _F_GOTO + else: + ipstruct.flags &= ~_F_GOTO + self._xt = xtables(rule.nfproto) module = (self._is_standard_target() and - self._xt.find_target('standard') or + self._xt.find_target('') or self._xt.find_target(name)) real_name = module and getattr(module[0], 'real_name', None) or None if real_name: @@ -673,23 +747,36 @@ 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): + name = self._get_alias_name() + if name is None: + return + 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) 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 @@ -706,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 @@ -720,9 +809,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() @@ -786,6 +872,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): """ @@ -860,6 +950,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", @@ -885,10 +976,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 @@ -915,11 +1006,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=goto) self.target = target return target @@ -987,9 +1078,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: @@ -1003,8 +1091,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 @@ -1048,9 +1139,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: @@ -1065,6 +1153,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 @@ -1102,10 +1194,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 +1236,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 @@ -1160,7 +1254,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 @@ -1171,16 +1268,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] @@ -1196,6 +1297,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)) @@ -1248,11 +1358,12 @@ 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 - #targetsz = entry.next_offset - entry.target_offset + # targetsz = entry.next_offset - entry.target_offset # iterate over matches to create blob if matchsz: @@ -1313,10 +1424,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): @@ -1385,7 +1497,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") @@ -1416,7 +1529,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): @@ -1459,10 +1577,14 @@ 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() + 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) @@ -1577,7 +1699,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,9 +1740,10 @@ 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)" % + raise IPTCError("can't set policy %s on chain %s: %s" % (policy, chain, self.strerror())) @autocommit @@ -1640,37 +1764,41 @@ 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)" % + raise IPTCError("can't append entry to chain %s: %s" % (chain, self.strerror())) @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)" % + raise IPTCError("can't insert entry into chain %s: %s" % (chain, self.strerror())) @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)" % + raise IPTCError("can't replace entry in chain %s: %s" % (chain, self.strerror())) @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)" % + raise IPTCError("can't delete entry from chain %s: %s" % (chain, self.strerror())) def first_rule(self, chain): diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index c186845..e0d8787 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 @@ -20,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 @@ -196,9 +199,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 +249,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 @@ -274,7 +277,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 @@ -413,22 +416,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): @@ -445,10 +445,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 @@ -457,23 +457,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): @@ -490,10 +486,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 @@ -504,10 +500,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 & @@ -517,6 +514,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] @@ -568,12 +568,16 @@ 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() + 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) diff --git a/iptc/util.py b/iptc/util.py index fc8cfe6..6c1592c 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -1,6 +1,9 @@ import re +import os +import sys import ctypes import ctypes.util +from itertools import product from subprocess import Popen, PIPE from sys import version_info try: @@ -10,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): @@ -19,6 +28,10 @@ def _insert_ko(modprobe, modname): def _load_ko(modname): + # only try to load modules on kernels that support them + if not os.path.exists("/proc/modules"): + return (0, None) + # this will return the full path for the modprobe binary modprobe = "/sbin/modprobe" try: @@ -44,14 +57,17 @@ 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) return lib # probably we have been installed in a virtualenv - import os - from distutils.sysconfig import get_python_lib try: lib = ctypes.CDLL(os.path.join(get_python_lib(), name), mode=ctypes.RTLD_GLOBAL) @@ -59,7 +75,6 @@ def _do_find_library(name): except: pass - import sys for p in sys.path: try: lib = ctypes.CDLL(os.path.join(p, name), mode=ctypes.RTLD_GLOBAL) @@ -70,11 +85,29 @@ def _do_find_library(name): def _find_library(*names): - ext = get_config_var('SO') - if version_info > (3, ) and version_info < (3, 4): - ext = '.cpython-%i%i' % (version_info.major, version_info.minor) + ext + exts = [] + if version_info >= (3, 3): + exts.append(get_config_var("EXT_SUFFIX")) + else: + exts.append(get_config_var('SO')) + + if version_info >= (3, 5): + exts.append('.so') + for name in names: - for n in (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(':') + libs = [os.path.join(*p) for p in product(libdirs, libnames)] + libs.extend(libnames) + else: + libs = libnames + for n in libs: + while os.path.islink(n): + n = os.path.realpath(n) lib = _do_find_library(n) if lib is not None: yield lib @@ -83,8 +116,24 @@ def _find_library(*names): def find_library(*names): for lib in _find_library(*names): major = 0 - m = re.search(r"\.so\.(\d+)", lib._name) + m = re.search(r"\.so\.(\d+).?", lib._name) if m: 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/version.py b/iptc/version.py index a00f4fb..f6a5157 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__ = "1.2.0" diff --git a/iptc/xtables.py b/iptc/xtables.py index 5cfbefc..024779c 100644 --- a/iptc/xtables.py +++ b/iptc/xtables.py @@ -6,7 +6,8 @@ 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 @@ -23,6 +24,7 @@ XTF_TRY_LOAD = 0x02 XTF_LOAD_MUST_SUCCEED = 0x03 + XTOPT_INVERT = 1 << 0 XTOPT_MAND = 1 << 1 XTOPT_MULTI = 1 << 2 @@ -83,7 +85,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() @@ -386,7 +390,62 @@ 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)), + + # 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)), + + # 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_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)), @@ -395,6 +454,8 @@ class _xtables_match_v10(ct.Structure): ("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), @@ -416,7 +477,9 @@ 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), + ("v12", _xtables_match_v12)] class _xtables_target_v1(ct.Structure): @@ -635,7 +698,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)), @@ -656,6 +720,63 @@ class _xtables_target_v10(ct.Structure): ("loaded", ct.c_uint)] +_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), ("v2", _xtables_target_v2), @@ -666,27 +787,40 @@ 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), + ("v12", _xtables_target_v12)] -class XTablesError(Exception): - """Raised when an xtables call fails for some reason.""" - -_libc, _ = find_library("c") +_libc = find_libc() _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"]: - if os.path.isdir(xtdir): - _xtables_libdir = xtdir - break + import re + ldconfig_path_regex = re.compile('^(/.*):($| \(.*\)$)') + import subprocess + ldconfig = subprocess.Popen( + ('/sbin/ldconfig', '-N', '-v'), + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 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 = os.path.join(ldconfig_path_regex_match.group(1), 'xtables') + 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") @@ -728,20 +862,31 @@ 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 +_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 + +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 = {} + + class xtables(object): _xtables_init_all = _lib_xtables.xtables_init_all _xtables_init_all.restype = ct.c_int @@ -762,14 +907,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) @@ -791,79 +936,31 @@ 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) - targetname = "_xtables_target_v%d" % (_xtables_version) + matchname = "_xtables_match_v%d" % (xtables_version) + targetname = "_xtables_target_v%d" % (xtables_version) try: self._match_struct = getattr(thismodule, matchname) self._target_struct = getattr(thismodule, targetname) except: raise XTablesError("unknown xtables version %d" % - (_xtables_version)) - - self._loaded_exts = [] + (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(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() 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" return name - def _loaded(self, name): - self._loaded_exts.append(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: @@ -909,37 +1006,53 @@ def _try_register(self, name): if self._try_extinit(name, lib): return - @preserve_globals + def _get_loaded_ext(self, name): + ext = _loaded_exts.get('%s___%s' % (self.proto, name), None) + return ext + + @set_nfproto 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) - match = xtables._xtables_find_match(name, XTF_TRY_LOAD, None) + match = xtables._xtables_find_match(name, XTF_DONT_LOAD, None) if not match: return match - self._loaded(name) - return ct.cast(match, ct.POINTER(self._match_struct)) + m = ct.cast(match, ct.POINTER(self._match_struct)) + self._loaded(m[0].name, m) + return m - @preserve_globals + @set_nfproto 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) - target = xtables._xtables_find_target(name, XTF_TRY_LOAD) + target = xtables._xtables_find_target(name, XTF_DONT_LOAD) if not target: return target - self._loaded(name) - return ct.cast(target, ct.POINTER(self._target_struct)) + t = ct.cast(target, ct.POINTER(self._target_struct)) + 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) @@ -965,7 +1078,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 @@ -1010,7 +1123,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 @@ -1091,7 +1204,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: @@ -1131,7 +1244,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: diff --git a/setup.py b/setup.py index ae3a46c..1929f93 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ """python-iptables setup script""" -from distutils.core import setup, Extension +from setuptools import setup, Extension # make pyflakes happy __pkgname__ = None @@ -14,6 +14,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", @@ -21,6 +23,7 @@ package_dir={"iptc": "iptc"}, ext_modules=[Extension("libxtwrapper", ["libxtwrapper/wrapper.c"])], + test_suite="tests", classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Console", @@ -36,10 +39,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", 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 86% rename from iptc/test/test_iptc.py rename to tests/test_iptc.py index 56e7e50..816df86 100755 --- a/iptc/test/test_iptc.py +++ b/tests/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() @@ -373,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() @@ -407,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 @@ -431,7 +449,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", "!"]: @@ -444,6 +462,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 = 33 + self.assertEquals(rule.protocol, '33') + rule.protocol = '!33' + self.assertEquals(rule.protocol, '!33') + def test_rule_compare(self): r1 = iptc.Rule6() r1.src = "::1/128" @@ -551,6 +576,25 @@ 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/128" + 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): + rule = iptc.Rule6() + rule.protocol = "tcp" + rule.src = "::1/128" + target = iptc.Target(rule, "ACCEPT") + rule.target = target + rule2 = iptc.easy.encode_iptc_rule({"protocol": "tcp", "src": "::1/128", "target": "ACCEPT"}, ipv6=True) + self.assertEqual(rule, rule2) class TestRule(unittest.TestCase): def setUp(self): @@ -580,13 +624,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] @@ -614,7 +658,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() @@ -668,6 +711,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 = 33 + self.assertEquals(rule.protocol, '33') + rule.protocol = '!33' + self.assertEquals(rule.protocol, '!33') + def test_rule_compare(self): r1 = iptc.Rule() r1.src = "127.0.0.2/255.255.255.0" @@ -736,6 +786,37 @@ 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{rule_num}".format(rule_num=rule_num) + rule.create_target("ACCEPT") + self.chain.insert_rule(rule) + + 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) + 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 len(rule.matches) == 1 + assert rule.matches[0].comment == "rule{rule_num}".format( + 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 = [] @@ -855,6 +936,25 @@ 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/32" + 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): + rule = iptc.Rule() + rule.protocol = "tcp" + rule.src = "127.0.0.1/32" + target = iptc.Target(rule, "ACCEPT") + rule.target = target + rule2 = iptc.easy.encode_iptc_rule({"protocol": "tcp", "src": "127.0.0.1/32", "target": "ACCEPT"}) + self.assertEqual(rule, rule2) def suite(): suite_table6 = unittest.TestLoader().loadTestsFromTestCase(TestTable6) diff --git a/iptc/test/test_matches.py b/tests/test_matches.py similarity index 79% rename from iptc/test/test_matches.py rename to tests/test_matches.py index 0b55524..d7c1995 100755 --- a/iptc/test/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): @@ -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() @@ -101,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): @@ -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() @@ -131,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: @@ -176,13 +180,15 @@ 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() 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: @@ -218,13 +224,15 @@ 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() 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: @@ -268,13 +276,15 @@ 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() 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): @@ -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() @@ -298,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): @@ -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,9 +378,10 @@ 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() - pass def test_state(self): self.match.state = "RELATED,ESTABLISHED" @@ -374,8 +389,10 @@ 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.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): @@ -398,9 +415,10 @@ 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() - pass def test_state(self): self.match.ctstate = "NEW,RELATED" @@ -409,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): @@ -432,6 +450,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() @@ -446,11 +466,42 @@ 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") +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) + # Remove counters when comparing rules + rule2_d.pop('counters', None) + self.assertEqual(rule_d, rule2_d) def suite(): suite_match = unittest.TestLoader().loadTestsFromTestCase(TestMatch) @@ -468,6 +519,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( @@ -476,7 +529,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(): diff --git a/iptc/test/test_targets.py b/tests/test_targets.py similarity index 95% rename from iptc/test/test_targets.py rename to tests/test_targets.py index a28f655..3587acc 100755 --- a/iptc/test/test_targets.py +++ b/tests/test_targets.py @@ -2,6 +2,7 @@ import unittest import iptc +from iptc.xtables import xtables_version is_table_available = iptc.is_table_available @@ -78,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() @@ -132,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() @@ -180,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() @@ -261,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() @@ -306,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() @@ -362,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() @@ -392,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() @@ -420,7 +435,7 @@ def suite(): suites.extend([suite_target, suite_cluster, suite_tos]) if is_table_available(iptc.Table.NAT): suites.extend([suite_redir, suite_masq, suite_dnat]) - if is_table_available(iptc.Table.RAW): + if is_table_available(iptc.Table.RAW) and xtables_version >= 10: suites.extend([suite_notrack, suite_ct]) return unittest.TestSuite(suites)