2015-09-22 18:41:53 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2015-10-31 18:31:08 +00:00
|
|
|
#
|
2015-09-22 18:41:53 +00:00
|
|
|
# (c) 2015, Linus Unnebäck <linus@folkdatorn.se>
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-09-22 18:41:53 +00:00
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
2015-09-28 17:02:07 +00:00
|
|
|
|
2016-12-27 07:27:22 +00:00
|
|
|
|
2017-03-14 16:07:22 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'core'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2015-09-22 18:41:53 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: iptables
|
|
|
|
short_description: Modify the systems iptables
|
|
|
|
requirements: []
|
|
|
|
version_added: "2.0"
|
|
|
|
author: Linus Unnebäck (@LinusU) <linus@folkdatorn.se>
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- Iptables is used to set up, maintain, and inspect the tables of IP packet
|
|
|
|
filter rules in the Linux kernel. This module does not handle the saving
|
|
|
|
and/or loading of rules, but rather only manipulates the current rules
|
|
|
|
that are present in memory. This is the same as the behaviour of the
|
|
|
|
"iptables" and "ip6tables" command which this module uses internally.
|
2015-10-15 09:28:19 +00:00
|
|
|
notes:
|
2015-10-16 17:49:23 +00:00
|
|
|
- This module just deals with individual rules. If you need advanced
|
|
|
|
chaining of rules the recommended way is to template the iptables restore
|
|
|
|
file.
|
2015-09-22 18:41:53 +00:00
|
|
|
options:
|
|
|
|
table:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- This option specifies the packet matching table which the command
|
|
|
|
should operate on. If the kernel is configured with automatic module
|
|
|
|
loading, an attempt will be made to load the appropriate module for
|
|
|
|
that table if it is not already there.
|
2015-09-22 18:41:53 +00:00
|
|
|
required: false
|
|
|
|
default: filter
|
|
|
|
choices: [ "filter", "nat", "mangle", "raw", "security" ]
|
|
|
|
state:
|
2015-12-18 18:40:43 +00:00
|
|
|
description:
|
2015-12-07 23:47:26 +00:00
|
|
|
- Whether the rule should be absent or present.
|
2015-09-22 18:41:53 +00:00
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: [ "present", "absent" ]
|
2016-05-16 20:25:52 +00:00
|
|
|
action:
|
|
|
|
version_added: "2.2"
|
|
|
|
description:
|
|
|
|
- Whether the rule should be appended at the bottom or inserted at the
|
|
|
|
top. If the rule already exists the chain won't be modified.
|
|
|
|
required: false
|
|
|
|
default: append
|
|
|
|
choices: [ "append", "insert" ]
|
2015-09-28 17:02:07 +00:00
|
|
|
ip_version:
|
2015-12-18 18:40:43 +00:00
|
|
|
description:
|
2015-12-07 23:47:26 +00:00
|
|
|
- Which version of the IP protocol this rule should apply to.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
|
|
|
default: ipv4
|
|
|
|
choices: [ "ipv4", "ipv6" ]
|
|
|
|
chain:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- "Chain to operate on. This option can either be the name of a user
|
|
|
|
defined chain or any of the builtin chains: 'INPUT', 'FORWARD',
|
2016-08-30 14:16:10 +00:00
|
|
|
'OUTPUT', 'PREROUTING', 'POSTROUTING', 'SECMARK', 'CONNSECMARK'."
|
|
|
|
required: false
|
2015-09-28 17:02:07 +00:00
|
|
|
protocol:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- The protocol of the rule or of the packet to check. The specified
|
|
|
|
protocol can be one of tcp, udp, udplite, icmp, esp, ah, sctp or the
|
|
|
|
special keyword "all", or it can be a numeric value, representing one
|
|
|
|
of these protocols or a different one. A protocol name from
|
|
|
|
/etc/protocols is also allowed. A "!" argument before the protocol
|
|
|
|
inverts the test. The number zero is equivalent to all. "all" will
|
|
|
|
match with all protocols and is taken as default when this option is
|
|
|
|
omitted.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
source:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- Source specification. Address can be either a network name,
|
|
|
|
a hostname, a network IP address (with /mask), or a plain IP address.
|
|
|
|
Hostnames will be resolved once only, before the rule is submitted to
|
|
|
|
the kernel. Please note that specifying any name to be resolved with
|
|
|
|
a remote query such as DNS is a really bad idea. The mask can be
|
|
|
|
either a network mask or a plain number, specifying the number of 1's
|
|
|
|
at the left side of the network mask. Thus, a mask of 24 is equivalent
|
|
|
|
to 255.255.255.0. A "!" argument before the address specification
|
2016-08-05 13:26:30 +00:00
|
|
|
inverts the sense of the address.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
destination:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- Destination specification. Address can be either a network name,
|
|
|
|
a hostname, a network IP address (with /mask), or a plain IP address.
|
|
|
|
Hostnames will be resolved once only, before the rule is submitted to
|
|
|
|
the kernel. Please note that specifying any name to be resolved with
|
|
|
|
a remote query such as DNS is a really bad idea. The mask can be
|
|
|
|
either a network mask or a plain number, specifying the number of 1's
|
|
|
|
at the left side of the network mask. Thus, a mask of 24 is equivalent
|
|
|
|
to 255.255.255.0. A "!" argument before the address specification
|
2016-08-05 13:26:30 +00:00
|
|
|
inverts the sense of the address.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
match:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- Specifies a match to use, that is, an extension module that tests for
|
|
|
|
a specific property. The set of matches make up the condition under
|
|
|
|
which a target is invoked. Matches are evaluated first to last if
|
|
|
|
specified as an array and work in short-circuit fashion, i.e. if one
|
|
|
|
extension yields false, evaluation will stop.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: []
|
2015-09-28 17:02:07 +00:00
|
|
|
jump:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- This specifies the target of the rule; i.e., what to do if the packet
|
|
|
|
matches it. The target can be a user-defined chain (other than the one
|
|
|
|
this rule is in), one of the special builtin targets which decide the
|
|
|
|
fate of the packet immediately, or an extension (see EXTENSIONS
|
2017-06-12 06:55:19 +00:00
|
|
|
below). If this option is omitted in a rule (and the goto parameter
|
2015-10-16 17:49:23 +00:00
|
|
|
is not used), then matching the rule will have no effect on the
|
|
|
|
packet's fate, but the counters on the rule will be incremented.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
goto:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- This specifies that the processing should continue in a user specified
|
|
|
|
chain. Unlike the jump argument return will not continue processing in
|
|
|
|
this chain but instead in the chain that called us via jump.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
in_interface:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- Name of an interface via which a packet was received (only for packets
|
|
|
|
entering the INPUT, FORWARD and PREROUTING chains). When the "!"
|
|
|
|
argument is used before the interface name, the sense is inverted. If
|
|
|
|
the interface name ends in a "+", then any interface which begins with
|
|
|
|
this name will match. If this option is omitted, any interface name
|
|
|
|
will match.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
out_interface:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- Name of an interface via which a packet is going to be sent (for
|
|
|
|
packets entering the FORWARD, OUTPUT and POSTROUTING chains). When the
|
|
|
|
"!" argument is used before the interface name, the sense is inverted.
|
|
|
|
If the interface name ends in a "+", then any interface which begins
|
|
|
|
with this name will match. If this option is omitted, any interface
|
|
|
|
name will match.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
fragment:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- This means that the rule only refers to second and further fragments
|
|
|
|
of fragmented packets. Since there is no way to tell the source or
|
|
|
|
destination ports of such a packet (or ICMP type), such a packet will
|
|
|
|
not match any rules which specify them. When the "!" argument precedes
|
|
|
|
fragment argument, the rule will only match head fragments, or
|
|
|
|
unfragmented packets.
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
set_counters:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- This enables the administrator to initialize the packet and byte
|
|
|
|
counters of a rule (during INSERT, APPEND, REPLACE operations).
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
source_port:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- "Source port or port range specification. This can either be a service
|
|
|
|
name or a port number. An inclusive range can also be specified, using
|
|
|
|
the format first:last. If the first port is omitted, '0' is assumed;
|
|
|
|
if the last is omitted, '65535' is assumed. If the first port is
|
|
|
|
greater than the second one they will be swapped."
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
destination_port:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- "Destination port or port range specification. This can either be
|
|
|
|
a service name or a port number. An inclusive range can also be
|
|
|
|
specified, using the format first:last. If the first port is omitted,
|
|
|
|
'0' is assumed; if the last is omitted, '65535' is assumed. If the
|
|
|
|
first port is greater than the second one they will be swapped."
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-09-28 17:02:07 +00:00
|
|
|
to_ports:
|
2015-10-16 17:49:23 +00:00
|
|
|
description:
|
|
|
|
- "This specifies a destination port or range of ports to use: without
|
|
|
|
this, the destination port is never altered. This is only valid if the
|
|
|
|
rule also specifies one of the following protocols: tcp, udp, dccp or
|
|
|
|
sctp."
|
2015-09-28 17:02:07 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2016-04-06 10:30:59 +00:00
|
|
|
to_destination:
|
|
|
|
version_added: "2.1"
|
|
|
|
description:
|
|
|
|
- "This specifies a destination address to use with DNAT: without
|
|
|
|
this, the destination address is never altered."
|
|
|
|
required: false
|
|
|
|
default: null
|
2016-05-26 16:51:31 +00:00
|
|
|
to_source:
|
|
|
|
version_added: "2.2"
|
|
|
|
description:
|
|
|
|
- "This specifies a source address to use with SNAT: without
|
|
|
|
this, the source address is never altered."
|
|
|
|
required: false
|
|
|
|
default: null
|
2016-03-09 16:04:58 +00:00
|
|
|
set_dscp_mark:
|
2016-03-09 16:20:34 +00:00
|
|
|
version_added: "2.1"
|
2016-03-09 16:04:58 +00:00
|
|
|
description:
|
|
|
|
- "This allows specifying a DSCP mark to be added to packets.
|
|
|
|
It takes either an integer or hex value. Mutually exclusive with
|
2016-03-31 22:37:37 +00:00
|
|
|
C(set_dscp_mark_class)."
|
2016-03-09 16:04:58 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2016-03-09 16:04:58 +00:00
|
|
|
set_dscp_mark_class:
|
2016-03-09 16:20:34 +00:00
|
|
|
version_added: "2.1"
|
2016-03-09 16:16:27 +00:00
|
|
|
description:
|
2016-03-09 16:04:58 +00:00
|
|
|
- "This allows specifying a predefined DiffServ class which will be
|
|
|
|
translated to the corresponding DSCP mark. Mutually exclusive with
|
2016-03-31 22:37:37 +00:00
|
|
|
C(set_dscp_mark)."
|
2016-03-16 18:06:56 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
2015-10-30 16:29:05 +00:00
|
|
|
comment:
|
|
|
|
description:
|
|
|
|
- "This specifies a comment that will be added to the rule"
|
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2015-11-02 09:36:58 +00:00
|
|
|
ctstate:
|
|
|
|
description:
|
2016-08-30 14:16:10 +00:00
|
|
|
- "ctstate is a list of the connection states to match in the conntrack
|
|
|
|
module.
|
|
|
|
Possible states are: 'INVALID', 'NEW', 'ESTABLISHED', 'RELATED',
|
|
|
|
'UNTRACKED', 'SNAT', 'DNAT'"
|
2015-11-02 09:36:58 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: []
|
2015-11-03 17:41:30 +00:00
|
|
|
limit:
|
|
|
|
description:
|
2016-08-30 14:16:10 +00:00
|
|
|
- "Specifies the maximum average number of matches to allow per second.
|
|
|
|
The number can specify units explicitly, using `/second', `/minute',
|
|
|
|
`/hour' or `/day', or parts of them (so `5/second' is the same as
|
|
|
|
`5/s')."
|
2015-11-03 17:41:30 +00:00
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2016-01-18 15:00:09 +00:00
|
|
|
limit_burst:
|
2016-02-02 04:55:41 +00:00
|
|
|
version_added: "2.1"
|
2016-01-18 15:00:09 +00:00
|
|
|
description:
|
|
|
|
- "Specifies the maximum burst before the above limit kicks in."
|
|
|
|
required: false
|
2016-03-16 18:06:56 +00:00
|
|
|
default: null
|
2016-01-28 08:27:31 +00:00
|
|
|
uid_owner:
|
2016-03-23 10:46:50 +00:00
|
|
|
version_added: "2.1"
|
2016-01-28 08:27:31 +00:00
|
|
|
description:
|
|
|
|
- "Specifies the UID or username to use in match by owner rule."
|
|
|
|
required: false
|
|
|
|
reject_with:
|
2016-03-23 10:46:50 +00:00
|
|
|
version_added: "2.1"
|
2016-01-28 08:27:31 +00:00
|
|
|
description:
|
|
|
|
- "Specifies the error packet type to return while rejecting."
|
|
|
|
required: false
|
2016-05-13 12:52:49 +00:00
|
|
|
icmp_type:
|
|
|
|
version_added: "2.2"
|
|
|
|
description:
|
2016-08-30 14:16:10 +00:00
|
|
|
- "This allows specification of the ICMP type, which can be a numeric
|
|
|
|
ICMP type, type/code pair, or one of the ICMP type names shown by the
|
|
|
|
command 'iptables -p icmp -h'"
|
|
|
|
required: false
|
|
|
|
flush:
|
|
|
|
version_added: "2.2"
|
|
|
|
description:
|
|
|
|
- "Flushes the specified table and chain of all rules. If no chain is
|
|
|
|
specified then the entire table is purged. Ignores all other
|
|
|
|
parameters."
|
2016-05-13 12:52:49 +00:00
|
|
|
required: false
|
2016-08-30 14:16:10 +00:00
|
|
|
policy:
|
|
|
|
version_added: "2.2"
|
|
|
|
description:
|
|
|
|
- "Set the policy for the chain to the given target. Valid targets are
|
|
|
|
ACCEPT, DROP, QUEUE, RETURN. Only built in chains can have policies.
|
|
|
|
This parameter requires the chain parameter. Ignores all other
|
|
|
|
parameters."
|
2015-09-22 18:41:53 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Block specific IP
|
2016-12-02 15:48:22 +00:00
|
|
|
- iptables:
|
|
|
|
chain: INPUT
|
|
|
|
source: 8.8.8.8
|
|
|
|
jump: DROP
|
2015-09-22 18:41:53 +00:00
|
|
|
become: yes
|
|
|
|
|
|
|
|
# Forward port 80 to 8600
|
2016-12-02 15:48:22 +00:00
|
|
|
- iptables:
|
|
|
|
table: nat
|
|
|
|
chain: PREROUTING
|
|
|
|
in_interface: eth0
|
|
|
|
protocol: tcp
|
|
|
|
match: tcp
|
|
|
|
destination_port: 80
|
|
|
|
jump: REDIRECT
|
|
|
|
to_ports: 8600
|
|
|
|
comment: Redirect web traffic to port 8600
|
2015-09-22 18:41:53 +00:00
|
|
|
become: yes
|
2015-11-02 09:36:58 +00:00
|
|
|
|
|
|
|
# Allow related and established connections
|
2016-12-02 15:48:22 +00:00
|
|
|
- iptables:
|
|
|
|
chain: INPUT
|
|
|
|
ctstate: ESTABLISHED,RELATED
|
|
|
|
jump: ACCEPT
|
2015-11-02 09:36:58 +00:00
|
|
|
become: yes
|
2016-03-09 16:04:58 +00:00
|
|
|
|
|
|
|
# Tag all outbound tcp packets with DSCP mark 8
|
2016-12-02 15:48:22 +00:00
|
|
|
- iptables:
|
|
|
|
chain: OUTPUT
|
|
|
|
jump: DSCP
|
|
|
|
table: mangle
|
|
|
|
set_dscp_mark: 8
|
|
|
|
protocol: tcp
|
2016-03-09 16:04:58 +00:00
|
|
|
|
|
|
|
# Tag all outbound tcp packets with DSCP DiffServ class CS1
|
2016-12-02 15:48:22 +00:00
|
|
|
- iptables:
|
|
|
|
chain: OUTPUT
|
|
|
|
jump: DSCP
|
|
|
|
table: mangle
|
|
|
|
set_dscp_mark_class: CS1
|
|
|
|
protocol: tcp
|
2017-06-28 22:01:34 +00:00
|
|
|
|
|
|
|
# Set the policy for the INPUT chain to DROP
|
|
|
|
- iptables:
|
|
|
|
chain: INPUT
|
|
|
|
policy: DROP
|
2015-09-22 18:41:53 +00:00
|
|
|
'''
|
|
|
|
|
2017-06-28 22:01:34 +00:00
|
|
|
import re
|
|
|
|
|
2016-12-20 15:50:42 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 05:55:24 +00:00
|
|
|
|
|
|
|
BINS = dict(
|
|
|
|
ipv4='iptables',
|
|
|
|
ipv6='ip6tables',
|
|
|
|
)
|
|
|
|
|
|
|
|
ICMP_TYPE_OPTIONS = dict(
|
|
|
|
ipv4='--icmp-type',
|
|
|
|
ipv6='--icmpv6-type',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-09-28 17:02:07 +00:00
|
|
|
def append_param(rule, param, flag, is_list):
|
|
|
|
if is_list:
|
|
|
|
for item in param:
|
|
|
|
append_param(rule, item, flag, False)
|
|
|
|
else:
|
|
|
|
if param is not None:
|
|
|
|
rule.extend([flag, param])
|
|
|
|
|
2015-10-30 16:29:05 +00:00
|
|
|
|
2015-12-18 18:40:43 +00:00
|
|
|
def append_csv(rule, param, flag):
|
2016-01-18 15:11:13 +00:00
|
|
|
if param:
|
2015-12-18 18:40:43 +00:00
|
|
|
rule.extend([flag, ','.join(param)])
|
2015-09-28 17:02:07 +00:00
|
|
|
|
2015-11-02 09:36:58 +00:00
|
|
|
|
2015-12-18 18:40:43 +00:00
|
|
|
def append_match(rule, param, match):
|
2015-11-03 17:41:30 +00:00
|
|
|
if param:
|
2015-12-18 18:40:43 +00:00
|
|
|
rule.extend(['-m', match])
|
2015-11-03 17:41:30 +00:00
|
|
|
|
2015-11-02 09:36:58 +00:00
|
|
|
|
2016-01-26 13:10:52 +00:00
|
|
|
def append_jump(rule, param, jump):
|
|
|
|
if param:
|
|
|
|
rule.extend(['-j', jump])
|
|
|
|
|
|
|
|
|
2015-09-28 17:02:07 +00:00
|
|
|
def construct_rule(params):
|
|
|
|
rule = []
|
|
|
|
append_param(rule, params['protocol'], '-p', False)
|
|
|
|
append_param(rule, params['source'], '-s', False)
|
|
|
|
append_param(rule, params['destination'], '-d', False)
|
|
|
|
append_param(rule, params['match'], '-m', True)
|
|
|
|
append_param(rule, params['jump'], '-j', False)
|
2016-04-06 10:30:59 +00:00
|
|
|
append_param(rule, params['to_destination'], '--to-destination', False)
|
2016-05-26 16:51:31 +00:00
|
|
|
append_param(rule, params['to_source'], '--to-source', False)
|
2015-09-28 17:02:07 +00:00
|
|
|
append_param(rule, params['goto'], '-g', False)
|
|
|
|
append_param(rule, params['in_interface'], '-i', False)
|
|
|
|
append_param(rule, params['out_interface'], '-o', False)
|
|
|
|
append_param(rule, params['fragment'], '-f', False)
|
|
|
|
append_param(rule, params['set_counters'], '-c', False)
|
|
|
|
append_param(rule, params['source_port'], '--source-port', False)
|
|
|
|
append_param(rule, params['destination_port'], '--destination-port', False)
|
|
|
|
append_param(rule, params['to_ports'], '--to-ports', False)
|
2016-03-09 16:04:58 +00:00
|
|
|
append_param(rule, params['set_dscp_mark'], '--set-dscp', False)
|
2016-08-30 14:16:10 +00:00
|
|
|
append_param(
|
|
|
|
rule,
|
|
|
|
params['set_dscp_mark_class'],
|
|
|
|
'--set-dscp-class',
|
|
|
|
False)
|
2015-12-18 18:40:43 +00:00
|
|
|
append_match(rule, params['comment'], 'comment')
|
2015-10-30 16:29:05 +00:00
|
|
|
append_param(rule, params['comment'], '--comment', False)
|
2017-03-08 16:01:47 +00:00
|
|
|
if 'conntrack' in params['match']:
|
|
|
|
append_csv(rule, params['ctstate'], '--ctstate')
|
|
|
|
elif 'state' in params['match']:
|
|
|
|
append_csv(rule, params['ctstate'], '--state')
|
|
|
|
elif params['ctstate']:
|
|
|
|
append_match(rule, params['ctstate'], 'conntrack')
|
|
|
|
append_csv(rule, params['ctstate'], '--ctstate')
|
2016-01-18 15:00:09 +00:00
|
|
|
append_match(rule, params['limit'] or params['limit_burst'], 'limit')
|
2015-11-03 17:41:30 +00:00
|
|
|
append_param(rule, params['limit'], '--limit', False)
|
2016-01-18 15:00:09 +00:00
|
|
|
append_param(rule, params['limit_burst'], '--limit-burst', False)
|
2016-01-26 13:10:52 +00:00
|
|
|
append_match(rule, params['uid_owner'], 'owner')
|
|
|
|
append_param(rule, params['uid_owner'], '--uid-owner', False)
|
|
|
|
append_jump(rule, params['reject_with'], 'REJECT')
|
|
|
|
append_param(rule, params['reject_with'], '--reject-with', False)
|
2016-12-27 07:27:22 +00:00
|
|
|
append_param(
|
|
|
|
rule,
|
|
|
|
params['icmp_type'],
|
|
|
|
ICMP_TYPE_OPTIONS[params['ip_version']],
|
|
|
|
False)
|
2015-09-28 17:02:07 +00:00
|
|
|
return rule
|
|
|
|
|
|
|
|
|
2016-08-30 14:16:10 +00:00
|
|
|
def push_arguments(iptables_path, action, params, make_rule=True):
|
2015-09-22 18:41:53 +00:00
|
|
|
cmd = [iptables_path]
|
2015-09-28 17:02:07 +00:00
|
|
|
cmd.extend(['-t', params['table']])
|
|
|
|
cmd.extend([action, params['chain']])
|
2016-08-30 14:16:10 +00:00
|
|
|
if make_rule:
|
|
|
|
cmd.extend(construct_rule(params))
|
2015-09-22 18:41:53 +00:00
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2015-09-28 17:02:07 +00:00
|
|
|
def check_present(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-C', params)
|
2015-09-22 18:41:53 +00:00
|
|
|
rc, _, __ = module.run_command(cmd, check_rc=False)
|
|
|
|
return (rc == 0)
|
|
|
|
|
|
|
|
|
2015-09-28 17:02:07 +00:00
|
|
|
def append_rule(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-A', params)
|
2015-09-22 18:41:53 +00:00
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
|
2016-05-16 20:25:52 +00:00
|
|
|
def insert_rule(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-I', params)
|
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
|
2015-09-28 17:02:07 +00:00
|
|
|
def remove_rule(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-D', params)
|
2015-09-22 18:41:53 +00:00
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
|
2016-08-30 14:16:10 +00:00
|
|
|
def flush_table(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-F', params, make_rule=False)
|
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
|
|
|
|
def set_chain_policy(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-P', params, make_rule=False)
|
|
|
|
cmd.append(params['policy'])
|
|
|
|
module.run_command(cmd, check_rc=True)
|
|
|
|
|
|
|
|
|
2017-06-28 22:01:34 +00:00
|
|
|
def get_chain_policy(iptables_path, module, params):
|
|
|
|
cmd = push_arguments(iptables_path, '-L', params)
|
|
|
|
rc, out, _ = module.run_command(cmd, check_rc=True)
|
|
|
|
chain_header = out.split("\n")[0]
|
|
|
|
result = re.search(r'\(policy ([A-Z]+)\)', chain_header)
|
|
|
|
if result:
|
|
|
|
return result.group(1)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-09-22 18:41:53 +00:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
supports_check_mode=True,
|
|
|
|
argument_spec=dict(
|
2016-08-30 14:16:10 +00:00
|
|
|
table=dict(
|
|
|
|
required=False,
|
|
|
|
default='filter',
|
|
|
|
choices=['filter', 'nat', 'mangle', 'raw', 'security']),
|
|
|
|
state=dict(
|
|
|
|
required=False,
|
|
|
|
default='present',
|
|
|
|
choices=['present', 'absent']),
|
|
|
|
action=dict(
|
|
|
|
required=False,
|
|
|
|
default='append',
|
|
|
|
type='str',
|
|
|
|
choices=['append', 'insert']),
|
|
|
|
ip_version=dict(
|
|
|
|
required=False,
|
|
|
|
default='ipv4',
|
|
|
|
choices=['ipv4', 'ipv6']),
|
|
|
|
chain=dict(required=False, default=None, type='str'),
|
2015-09-28 17:02:07 +00:00
|
|
|
protocol=dict(required=False, default=None, type='str'),
|
|
|
|
source=dict(required=False, default=None, type='str'),
|
2016-05-26 16:51:31 +00:00
|
|
|
to_source=dict(required=False, default=None, type='str'),
|
2015-09-28 17:02:07 +00:00
|
|
|
destination=dict(required=False, default=None, type='str'),
|
2016-04-06 10:30:59 +00:00
|
|
|
to_destination=dict(required=False, default=None, type='str'),
|
2015-09-28 17:02:07 +00:00
|
|
|
match=dict(required=False, default=[], type='list'),
|
|
|
|
jump=dict(required=False, default=None, type='str'),
|
|
|
|
goto=dict(required=False, default=None, type='str'),
|
|
|
|
in_interface=dict(required=False, default=None, type='str'),
|
|
|
|
out_interface=dict(required=False, default=None, type='str'),
|
|
|
|
fragment=dict(required=False, default=None, type='str'),
|
|
|
|
set_counters=dict(required=False, default=None, type='str'),
|
|
|
|
source_port=dict(required=False, default=None, type='str'),
|
|
|
|
destination_port=dict(required=False, default=None, type='str'),
|
|
|
|
to_ports=dict(required=False, default=None, type='str'),
|
2016-08-30 14:16:10 +00:00
|
|
|
set_dscp_mark=dict(required=False, default=None, type='str'),
|
|
|
|
set_dscp_mark_class=dict(required=False, default=None, type='str'),
|
2015-10-31 18:31:08 +00:00
|
|
|
comment=dict(required=False, default=None, type='str'),
|
2015-11-03 17:03:00 +00:00
|
|
|
ctstate=dict(required=False, default=[], type='list'),
|
2015-11-03 17:47:28 +00:00
|
|
|
limit=dict(required=False, default=None, type='str'),
|
2016-01-18 15:00:09 +00:00
|
|
|
limit_burst=dict(required=False, default=None, type='str'),
|
2016-01-26 13:10:52 +00:00
|
|
|
uid_owner=dict(required=False, default=None, type='str'),
|
|
|
|
reject_with=dict(required=False, default=None, type='str'),
|
2016-05-13 12:52:49 +00:00
|
|
|
icmp_type=dict(required=False, default=None, type='str'),
|
2016-08-30 14:16:10 +00:00
|
|
|
flush=dict(required=False, default=False, type='bool'),
|
|
|
|
policy=dict(
|
|
|
|
required=False,
|
|
|
|
default=None,
|
|
|
|
type='str',
|
|
|
|
choices=['ACCEPT', 'DROP', 'QUEUE', 'RETURN']),
|
2015-09-22 18:41:53 +00:00
|
|
|
),
|
2016-03-09 16:04:58 +00:00
|
|
|
mutually_exclusive=(
|
|
|
|
['set_dscp_mark', 'set_dscp_mark_class'],
|
2016-08-30 14:16:10 +00:00
|
|
|
['flush', 'policy'],
|
2016-03-09 16:04:58 +00:00
|
|
|
),
|
2015-09-22 18:41:53 +00:00
|
|
|
)
|
|
|
|
args = dict(
|
|
|
|
changed=False,
|
|
|
|
failed=False,
|
2015-09-28 17:02:07 +00:00
|
|
|
ip_version=module.params['ip_version'],
|
2015-09-22 18:41:53 +00:00
|
|
|
table=module.params['table'],
|
|
|
|
chain=module.params['chain'],
|
2016-08-30 14:16:10 +00:00
|
|
|
flush=module.params['flush'],
|
2015-09-28 17:02:07 +00:00
|
|
|
rule=' '.join(construct_rule(module.params)),
|
2015-09-22 18:41:53 +00:00
|
|
|
state=module.params['state'],
|
|
|
|
)
|
2016-08-30 14:16:10 +00:00
|
|
|
|
2015-09-28 17:02:07 +00:00
|
|
|
ip_version = module.params['ip_version']
|
|
|
|
iptables_path = module.get_bin_path(BINS[ip_version], True)
|
2016-08-30 14:16:10 +00:00
|
|
|
|
|
|
|
# Check if chain option is required
|
|
|
|
if args['flush'] is False and args['chain'] is None:
|
2016-12-20 15:50:42 +00:00
|
|
|
module.fail_json( msg="Either chain or flush parameter must be specified.")
|
2016-08-30 14:16:10 +00:00
|
|
|
|
|
|
|
# Flush the table
|
2017-06-28 22:01:34 +00:00
|
|
|
if args['flush'] is True:
|
2016-12-20 15:50:42 +00:00
|
|
|
args['changed'] = True
|
|
|
|
if not module.check_mode:
|
|
|
|
flush_table(iptables_path, module, module.params)
|
2016-08-30 14:16:10 +00:00
|
|
|
|
|
|
|
# Set the policy
|
2016-12-20 15:50:42 +00:00
|
|
|
elif module.params['policy']:
|
2017-06-28 22:01:34 +00:00
|
|
|
current_policy = get_chain_policy(iptables_path, module, module.params)
|
|
|
|
if not current_policy:
|
|
|
|
module.fail_json(msg='Can\'t detect current policy')
|
|
|
|
|
|
|
|
changed = current_policy != module.params['policy']
|
|
|
|
args['changed'] = changed
|
|
|
|
if changed and not module.check_mode:
|
2016-12-20 15:50:42 +00:00
|
|
|
set_chain_policy(iptables_path, module, module.params)
|
|
|
|
|
2015-09-22 18:41:53 +00:00
|
|
|
else:
|
2016-12-20 15:50:42 +00:00
|
|
|
insert = (module.params['action'] == 'insert')
|
|
|
|
rule_is_present = check_present(iptables_path, module, module.params)
|
|
|
|
should_be_present = (args['state'] == 'present')
|
|
|
|
|
|
|
|
# Check if target is up to date
|
|
|
|
args['changed'] = (rule_is_present != should_be_present)
|
2017-06-28 22:01:34 +00:00
|
|
|
if args['changed'] is False:
|
|
|
|
# Target is already up to date
|
|
|
|
module.exit_json(**args)
|
2016-12-20 15:50:42 +00:00
|
|
|
|
2017-06-28 22:01:34 +00:00
|
|
|
# Check only; don't modify
|
|
|
|
if not module.check_mode:
|
2016-12-20 15:50:42 +00:00
|
|
|
if should_be_present:
|
|
|
|
if insert:
|
|
|
|
insert_rule(iptables_path, module, module.params)
|
|
|
|
else:
|
|
|
|
append_rule(iptables_path, module, module.params)
|
|
|
|
else:
|
2017-06-28 22:01:34 +00:00
|
|
|
insert = (module.params['action'] == 'insert')
|
|
|
|
rule_is_present = check_present(iptables_path, module, module.params)
|
|
|
|
should_be_present = (args['state'] == 'present')
|
|
|
|
|
|
|
|
# Check if target is up to date
|
|
|
|
args['changed'] = (rule_is_present != should_be_present)
|
|
|
|
|
|
|
|
if args['changed'] and not module.check_mode:
|
|
|
|
if should_be_present:
|
|
|
|
if insert:
|
|
|
|
insert_rule(iptables_path, module, module.params)
|
|
|
|
else:
|
|
|
|
append_rule(iptables_path, module, module.params)
|
|
|
|
else:
|
|
|
|
remove_rule(iptables_path, module, module.params)
|
2015-09-22 18:41:53 +00:00
|
|
|
|
|
|
|
module.exit_json(**args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|