File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/firewalld.cpython-310.pyc
o
�N�g g � @ s� d Z ddlZddlZddlmZ ddlmZ e�e �Z
G dd� d�Zdd� Zd d
� Z
ddd
�Zddd�Z ddd�ZdS )a-
Management of firewalld
.. versionadded:: 2015.8.0
The following example applies changes to the public zone, blocks echo-reply
and echo-request packets, does not set the zone to be the default, enables
masquerading, and allows ports 22/tcp and 25/tcp.
It will be applied permanently and directly before restart/reload.
.. code-block:: yaml
public:
firewalld.present:
- name: public
- block_icmp:
- echo-reply
- echo-request
- default: False
- masquerade: True
- ports:
- 22/tcp
- 25/tcp
The following example applies changes to the public zone, enables
masquerading and configures port forwarding TCP traffic from port 22
to 2222, and forwards TCP traffic from port 80 to 443 at 192.168.0.1.
.. code-block:: yaml
my_zone:
firewalld.present:
- name: public
- masquerade: True
- port_fwd:
- 22:2222:tcp
- 80:443:tcp:192.168.0.1
The following example binds the public zone to interface eth0 and to all
packets coming from the 192.168.1.0/24 subnet. It also removes the zone
from all other interfaces or sources.
.. code-block:: yaml
public:
firewalld.present:
- name: public
- interfaces:
- eth0
- sources:
- 192.168.1.0/24
Here, we define a new service that encompasses TCP ports 4505 4506:
.. code-block:: yaml
saltmaster:
firewalld.service:
- name: saltmaster
- ports:
- 4505/tcp
- 4506/tcp
To make this new service available in a zone, the following can be used, which
would allow access to the salt master from the 10.0.0.0/8 subnet:
.. code-block:: yaml
saltzone:
firewalld.present:
- name: saltzone
- services:
- saltmaster
- sources:
- 10.0.0.0/8
Another way of implementing the same rule above using rich rules is demonstrated
here:
.. code-block:: yaml
saltzone:
firewalld.present:
- name: saltzone
- rich_rules:
- rule service name="saltmaster" accept
- sources:
- 10.0.0.0/8
The format of rich rules is the same as:
.. code-block:: shell
firewall-cmd --list-rich-rules
with an example output of:
.. code-block:: text
rule protocol value="icmp" accept
rule protocol value="ipv6-icmp" accept
rule service name="snmp" accept
� N)�CommandExecutionError)�nestedc @ s8 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� ZdS )
�ForwardingMappingz
Represents a port forwarding statement mapping a local port to a remote
port for a specific protocol (TCP or UDP)
c C s || _ || _|| _|| _d S �N��srcport�destport�protocol�destaddr)�selfr r r r
� r �I/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/firewalld.py�__init__x s
zForwardingMapping.__init__c C s0 | j |j ko| j|jko| j|jko| j|jkS r r �r �otherr r r
�__eq__~ s
�
�
�zForwardingMapping.__eq__c C s | � |� S r )r r r r r
�__ne__� s zForwardingMapping.__ne__c C s( t | j�t | j�A t | j�A t | j�A S r )�hashr r r r
�r r r r
�__hash__� s ����zForwardingMapping.__hash__c C s | j | j| j| jd�S )zL
Returns a pretty dictionary meant for command line output.
)�Source port�Destination port�Protocol�Destination addressr r r r r
�todict� s
�zForwardingMapping.todictN) �__name__�
__module__�__qualname__�__doc__r r r r r r r r r
r r s r c C sH t | �d��dkr| �d�\}}}}n
| �d�\}}}d}t||||�S )z�
Parses a port forwarding statement in the form used by this state:
from_port:to_port:protocol[:destination]
and returns a ForwardingMapping object
�:� � )�len�splitr )�mappingr r r r
r r r
�_parse_forward� s
r% c C s t jj�d�r dS dS )z.
Ensure the firewall-cmd is available
zfirewall-cmdT)FzCfirewall-cmd is not available, firewalld is probably not installed.)�salt�utils�path�whichr r r r
�__virtual__� s r* Fc C sB t | ||||||||| |
|||
|||�}|d i krtd � |S )a
Ensure a zone has specific attributes.
name
The zone to modify.
default : None
Set this zone as the default zone if ``True``.
masquerade : False
Enable or disable masquerade for a zone.
block_icmp : None
List of ICMP types to block in the zone.
prune_block_icmp : False
If ``True``, remove all but the specified block_icmp from the zone.
ports : None
List of ports to add to the zone.
prune_ports : False
If ``True``, remove all but the specified ports from the zone.
port_fwd : None
List of port forwards to add to the zone.
prune_port_fwd : False
If ``True``, remove all but the specified port_fwd from the zone.
services : None
List of services to add to the zone.
prune_services : False
If ``True``, remove all but the specified services from the zone.
.. note:: Currently defaults to True for compatibility, but will be changed to False in a future release.
interfaces : None
List of interfaces to add to the zone.
prune_interfaces : False
If ``True``, remove all but the specified interfaces from the zone.
sources : None
List of sources to add to the zone.
prune_sources : False
If ``True``, remove all but the specified sources from the zone.
rich_rules : None
List of rich rules to add to the zone.
prune_rich_rules : False
If ``True``, remove all but the specified rich rules from the zone.
�changes�firewalld.reload_rules)�_present�__salt__)�name�
block_icmp�prune_block_icmp�default�
masquerade�ports�prune_ports�port_fwd�prune_port_fwd�services�prune_services�
interfaces�prune_interfaces�sources�
prune_sources�
rich_rules�prune_rich_rules�retr r r
�present� s, J�
rA c
C sX | di dd�}| t d � vrt d | dd� |pg }zt d | �}W n ty= } zd|� �|d <