mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
270 lines
9.1 KiB
Python
270 lines
9.1 KiB
Python
11 years ago
|
#!/usr/bin/python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
11 years ago
|
# (c) 2014, Ahti Kitsik <ak@ahtik.com>
|
||
11 years ago
|
# (c) 2014, Jarno Keskikangas <jarno.keskikangas@gmail.com>
|
||
11 years ago
|
# (c) 2013, Aleksey Ovcharenko <aleksey.ovcharenko@gmail.com>
|
||
|
# (c) 2013, James Martin <jmartin@basho.com>
|
||
|
#
|
||
11 years ago
|
# This file is part of Ansible
|
||
|
#
|
||
11 years ago
|
# Ansible is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# Ansible is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||
11 years ago
|
|
||
11 years ago
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: ufw
|
||
11 years ago
|
short_description: Manage firewall with UFW
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Manage firewall with UFW.
|
||
11 years ago
|
version_added: 1.6
|
||
11 years ago
|
author: Aleksey Ovcharenko, Jarno Keskikangas, Ahti Kitsik
|
||
11 years ago
|
notes:
|
||
|
- See C(man ufw) for more examples.
|
||
|
requirements:
|
||
|
- C(ufw) package
|
||
11 years ago
|
options:
|
||
11 years ago
|
state:
|
||
|
description:
|
||
|
- C(enabled) reloads firewall and enables firewall on boot.
|
||
|
- C(disabled) unloads firewall and disables firewall on boot.
|
||
|
- C(reloaded) reloads firewall.
|
||
11 years ago
|
- C(reset) disables and resets firewall to installation defaults.
|
||
11 years ago
|
required: false
|
||
11 years ago
|
choices: ['enabled', 'disabled', 'reloaded', 'reset']
|
||
11 years ago
|
policy:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Change the default policy for incoming or outgoing traffic.
|
||
11 years ago
|
required: false
|
||
11 years ago
|
alias: default
|
||
11 years ago
|
choices: ['allow', 'deny', 'reject']
|
||
11 years ago
|
direction:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Select direction for a rule or default policy command.
|
||
11 years ago
|
required: false
|
||
11 years ago
|
choices: ['in', 'out', 'incoming', 'outgoing']
|
||
|
logging:
|
||
|
description:
|
||
|
- Toggles logging. Logged packets use the LOG_KERN syslog facility.
|
||
|
choices: ['on', 'off', 'low', 'medium', 'high', 'full']
|
||
11 years ago
|
required: false
|
||
11 years ago
|
insert:
|
||
|
description:
|
||
|
- Insert the corresponding rule as rule number NUM
|
||
|
required: false
|
||
11 years ago
|
rule:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Add firewall rule
|
||
11 years ago
|
required: false
|
||
11 years ago
|
choices: ['allow', 'deny', 'reject', 'limit']
|
||
11 years ago
|
log:
|
||
|
description:
|
||
|
- Log new connections matched to this rule
|
||
|
required: false
|
||
11 years ago
|
choices: ['yes', 'no']
|
||
11 years ago
|
from_ip:
|
||
|
description:
|
||
|
- Source IP address.
|
||
|
required: false
|
||
11 years ago
|
aliases: ['from', 'src']
|
||
11 years ago
|
default: 'any'
|
||
|
from_port:
|
||
|
description:
|
||
|
- Source port.
|
||
|
required: false
|
||
|
to_ip:
|
||
|
description:
|
||
|
- Destination IP address.
|
||
|
required: false
|
||
11 years ago
|
aliases: ['to', 'dest']
|
||
11 years ago
|
default: 'any'
|
||
|
to_port:
|
||
|
description:
|
||
|
- Destination port.
|
||
|
required: false
|
||
|
aliases: ['port']
|
||
|
proto:
|
||
|
description:
|
||
|
- TCP/IP protocol.
|
||
11 years ago
|
choices: ['any', 'tcp', 'udp', 'ipv6', 'esp', 'ah']
|
||
11 years ago
|
required: false
|
||
11 years ago
|
name:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Use profile located in C(/etc/ufw/applications.d)
|
||
11 years ago
|
required: false
|
||
11 years ago
|
aliases: ['app']
|
||
|
delete:
|
||
|
description:
|
||
|
- Delete rule.
|
||
|
required: false
|
||
|
choices: ['yes', 'no']
|
||
11 years ago
|
interface:
|
||
|
description:
|
||
|
- Specify interface for rule.
|
||
|
required: false
|
||
|
aliases: ['if']
|
||
11 years ago
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
|
# Allow everything and enable UFW
|
||
11 years ago
|
ufw: state=enabled policy=allow
|
||
|
|
||
|
# Set logging
|
||
|
ufw: logging=on
|
||
11 years ago
|
|
||
|
# Sometimes it is desirable to let the sender know when traffic is
|
||
|
# being denied, rather than simply ignoring it. In these cases, use
|
||
11 years ago
|
# reject instead of deny. In addition, log rejected connections:
|
||
|
ufw: rule=reject port=auth log=yes
|
||
11 years ago
|
|
||
|
# ufw supports connection rate limiting, which is useful for protecting
|
||
|
# against brute-force login attacks. ufw will deny connections if an IP
|
||
|
# address has attempted to initiate 6 or more connections in the last
|
||
|
# 30 seconds. See http://www.debian-administration.org/articles/187
|
||
|
# for details. Typical usage is:
|
||
11 years ago
|
ufw: rule=limit port=ssh proto=tcp
|
||
11 years ago
|
|
||
|
# Allow OpenSSH
|
||
11 years ago
|
ufw: rule=allow name=OpenSSH
|
||
|
|
||
|
# Delete OpenSSH rule
|
||
|
ufw: rule=allow name=OpenSSH delete=yes
|
||
11 years ago
|
|
||
|
# Deny all access to port 53:
|
||
11 years ago
|
ufw: rule=deny port=53
|
||
11 years ago
|
|
||
|
# Allow all access to tcp port 80:
|
||
11 years ago
|
ufw: rule=allow port=80 proto=tcp
|
||
11 years ago
|
|
||
|
# Allow all access from RFC1918 networks to this host:
|
||
11 years ago
|
ufw: rule=allow src={{ item }}
|
||
11 years ago
|
with_items:
|
||
|
- 10.0.0.0/8
|
||
|
- 172.16.0.0/12
|
||
|
- 192.168.0.0/16
|
||
|
|
||
|
# Deny access to udp port 514 from host 1.2.3.4:
|
||
11 years ago
|
ufw: rule=deny proto=udp src=1.2.3.4 port=514
|
||
11 years ago
|
|
||
11 years ago
|
# Allow incoming access to eth0 from 1.2.3.5 port 5469 to 1.2.3.4 port 5469
|
||
|
ufw: rule=allow interface=eth0 direction=in proto=udp src=1.2.3.5 from_port=5469 dest=1.2.3.4 to_port=5469
|
||
11 years ago
|
|
||
|
# Deny all traffic from the IPv6 2001:db8::/32 to tcp port 25 on this host.
|
||
|
# Note that IPv6 must be enabled in /etc/default/ufw for IPv6 firewalling to work.
|
||
11 years ago
|
ufw: rule=deny proto=tcp src=2001:db8::/32 port=25
|
||
11 years ago
|
'''
|
||
|
|
||
11 years ago
|
from operator import itemgetter
|
||
|
|
||
11 years ago
|
|
||
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
11 years ago
|
state = dict(default=None, choices=['enabled', 'disabled', 'reloaded', 'reset']),
|
||
11 years ago
|
default = dict(default=None, aliases=['policy'], choices=['allow', 'deny', 'reject']),
|
||
11 years ago
|
logging = dict(default=None, choices=['on', 'off', 'low', 'medium', 'high', 'full']),
|
||
|
direction = dict(default=None, choices=['in', 'incoming', 'out', 'outgoing']),
|
||
11 years ago
|
delete = dict(default=False, type='bool'),
|
||
11 years ago
|
insert = dict(default=None),
|
||
11 years ago
|
rule = dict(default=None, choices=['allow', 'deny', 'reject', 'limit']),
|
||
|
interface = dict(default=None, aliases=['if']),
|
||
11 years ago
|
log = dict(default=False, type='bool'),
|
||
11 years ago
|
from_ip = dict(default='any', aliases=['src', 'from']),
|
||
|
from_port = dict(default=None),
|
||
|
to_ip = dict(default='any', aliases=['dest', 'to']),
|
||
|
to_port = dict(default=None, aliases=['port']),
|
||
11 years ago
|
proto = dict(default=None, aliases=['protocol'], choices=['any', 'tcp', 'udp', 'ipv6', 'esp', 'ah']),
|
||
11 years ago
|
app = dict(default=None, aliases=['name'])
|
||
11 years ago
|
),
|
||
11 years ago
|
supports_check_mode = True,
|
||
11 years ago
|
mutually_exclusive = [['app', 'proto', 'logging']]
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
cmds = []
|
||
|
|
||
|
def execute(cmd):
|
||
|
cmd = ' '.join(map(itemgetter(-1), filter(itemgetter(0), cmd)))
|
||
11 years ago
|
|
||
11 years ago
|
cmds.append(cmd)
|
||
|
(rc, out, err) = module.run_command(cmd)
|
||
|
|
||
|
if rc != 0:
|
||
|
module.fail_json(msg=err or out)
|
||
|
|
||
|
params = module.params
|
||
|
|
||
|
# Ensure at least one of the command arguments are given
|
||
|
command_keys = ['state', 'default', 'rule', 'logging']
|
||
|
commands = dict((key, params[key]) for key in command_keys if params[key])
|
||
|
|
||
|
if len(commands) < 1:
|
||
|
module.fail_json(msg="Not any of the command arguments %s given" % commands)
|
||
|
|
||
11 years ago
|
if('interface' in params and 'direction' not in params):
|
||
|
module.fail_json(msg="Direction must be specified when creating a rule on an interface")
|
||
|
|
||
11 years ago
|
# Ensure ufw is available
|
||
|
ufw_bin = module.get_bin_path('ufw', True)
|
||
|
|
||
11 years ago
|
# Save the pre state and rules in order to recognize changes
|
||
11 years ago
|
(_, pre_state, _) = module.run_command(ufw_bin + ' status verbose')
|
||
11 years ago
|
(_, pre_rules, _) = module.run_command("grep '^### tuple' /lib/ufw/user*.rules")
|
||
11 years ago
|
|
||
|
# Execute commands
|
||
|
for (command, value) in commands.iteritems():
|
||
|
cmd = [[ufw_bin], [module.check_mode, '--dry-run']]
|
||
|
|
||
|
if command == 'state':
|
||
|
states = { 'enabled': 'enable', 'disabled': 'disable',
|
||
11 years ago
|
'reloaded': 'reload', 'reset': 'reset' }
|
||
11 years ago
|
execute(cmd + [['-f'], [states[value]]])
|
||
|
|
||
|
elif command == 'logging':
|
||
11 years ago
|
execute(cmd + [[command], [value]])
|
||
11 years ago
|
|
||
|
elif command == 'default':
|
||
|
execute(cmd + [[command], [value], [params['direction']]])
|
||
|
|
||
|
elif command == 'rule':
|
||
|
# Rules are constructed according to the long format
|
||
|
#
|
||
|
# ufw [--dry-run] [delete] [insert NUM] allow|deny|reject|limit [in|out on INTERFACE] [log|log-all] \
|
||
|
# [from ADDRESS [port PORT]] [to ADDRESS [port PORT]] \
|
||
|
# [proto protocol] [app application]
|
||
|
cmd.append([module.boolean(params['delete']), 'delete'])
|
||
11 years ago
|
cmd.append([params['insert'], "insert %s" % params['insert']])
|
||
11 years ago
|
cmd.append([value])
|
||
|
cmd.append([module.boolean(params['log']), 'log'])
|
||
|
|
||
|
for (key, template) in [('direction', "%s" ), ('interface', "on %s" ),
|
||
|
('from_ip', "from %s" ), ('from_port', "port %s" ),
|
||
|
('to_ip', "to %s" ), ('to_port', "port %s" ),
|
||
|
('proto', "proto %s"), ('app', "app '%s'")]:
|
||
|
|
||
|
value = params[key]
|
||
|
cmd.append([value, template % (value)])
|
||
|
|
||
|
execute(cmd)
|
||
|
|
||
|
# Get the new state
|
||
|
(_, post_state, _) = module.run_command(ufw_bin + ' status verbose')
|
||
11 years ago
|
(_, post_rules, _) = module.run_command("grep '^### tuple' /lib/ufw/user*.rules")
|
||
|
changed = (pre_state != post_state) or (pre_rules != post_rules)
|
||
11 years ago
|
|
||
|
return module.exit_json(changed=changed, commands=cmds, msg=post_state.rstrip())
|
||
11 years ago
|
|
||
11 years ago
|
# import module snippets
|
||
|
from ansible.module_utils.basic import *
|
||
11 years ago
|
|
||
|
main()
|