HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/firewalld.cpython-310.pyc
o

�N�g	g�@s�dZddlZddlZddlmZddlmZe�e	�Z
Gdd�d�Zdd�Zd	d
�Z
																ddd
�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@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�ForwardingMappingz
    Represents a port forwarding statement mapping a local port to a remote
    port for a specific protocol (TCP or UDP)
    cCs||_||_||_||_dS�N��srcport�destport�protocol�destaddr)�selfrrr	r
�r�I/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/firewalld.py�__init__xs
zForwardingMapping.__init__cCs0|j|jko|j|jko|j|jko|j|jkSrr�r�otherrrr
�__eq__~s
�
�
�zForwardingMapping.__eq__cCs|�|�Sr)rrrrr
�__ne__�szForwardingMapping.__ne__cCs(t|j�t|j�At|j�At|j�ASr)�hashrrr	r
�rrrr
�__hash__�s����zForwardingMapping.__hash__cCs|j|j|j|jd�S)zL
        Returns a pretty dictionary meant for command line output.
        )�Source port�Destination port�Protocol�Destination addressrrrrr
�todict�s
�zForwardingMapping.todictN)	�__name__�
__module__�__qualname__�__doc__rrrrrrrrr
rrsrcCsHt|�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)�mappingrrr	r
rrr
�_parse_forward�s
r%cCstjj�d�r	dSdS)z.
    Ensure the firewall-cmd is available
    zfirewall-cmdT)FzCfirewall-cmd is not available, firewalld is probably not installed.)�salt�utils�path�whichrrrr
�__virtual__�sr*FcCsBt||||||||||	|
|||
|||�}|dikrtd�|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�
rAc
CsX|didd�}|td�vrtd|dd�|pg}ztd|�}Wnty=}zd|��|d	<|WYd
}~Sd
}~wwt|�t|�}t|�t|�}|D]-}tds}z	td||�WqPty|}zd|��|d	<|WYd
}~Sd
}~wwqP|D]-}tds�z	td
||�Wq�ty�}zd|��|d	<|WYd
}~Sd
}~wwq�|s�|r�|d�d||d�i�|p�g}ztd|�}	Wnty�}zd|��|d	<|WYd
}~Sd
}~wwt|�t|	�}
t|	�t|�}|
D]/}td�s'z	td||�Wq�t�y&}zd|��|d	<|WYd
}~Sd
}~wwq�|D]1}td�sZz
td||�W�q*t�yY}zd|��|d	<|WYd
}~Sd
}~ww�q*|
�sb|�rn|d�d|	|d�i�|dik�rztd�d|d<|dik�r�d|�d�|d	<|Std�r�d
|d<d|�d�|d	<|Sd|�d�|d	<|S)z{
    Ensure the service exists and encompasses the specified ports and
    protocols.

    .. versionadded:: 2016.11.0
    Fr!�r/�resultr+�commentzfirewalld.get_serviceszfirewalld.new_service)Zrestartzfirewalld.get_service_ports�Error: rDN�testzfirewalld.add_service_portzfirewalld.remove_service_portr+r4��old�newzfirewalld.get_service_protocolszfirewalld.add_service_protocolz!firewalld.remove_service_protocol�	protocolsr,TrC�'�"' is already in the desired state.�Configuration for 'z' will change.�' was configured.)r.r�set�__opts__�update)
r/r4rJr@�_current_ports�err�	new_ports�	old_ports�portZ_current_protocolsZ
new_protocolsZ
old_protocolsr	rrr
�service s�����������
���
����

rWc5Cs�|didd�}z	tddd�}Wnty+}zd|��|d<|WYd	}~Sd	}~ww||vrdtd
sXztd|�WntyW}zd|��|d<|WYd	}~Sd	}~ww|d�|||d
�i�|si|�ro|plg}g}g}z
td|dd�}Wnty�}zd|��|d<|WYd	}~Sd	}~ww|�rz	tddd�}Wnty�}zd|��|d<|WYd	}~Sd	}~wwt|�t|�D]
}t�d|�|�|�q�t|�t|�}|D]1}td
�sztd||dd�Wq�t�y}zd|��|d<|WYd	}~Sd	}~wwq�|�rRt|�t|�}|D]3}td
�sPztd||dd�W�qt�yO}zd|��|d<|WYd	}~Sd	}~ww�q|�sX|�ro|�sct|t|�B�}|d�d||d
�i�|�r�ztd�}Wnt�y�}zd|��|d<|WYd	}~Sd	}~ww||k�r�td
�s�ztd|�Wnt�y�}zd|��|d<|WYd	}~Sd	}~ww|d�d||d
�i�z
td|dd�}Wnt�y�}zd|��|d<|WYd	}~Sd	}~ww|�r7|�s7td
�s*z
td|dd�Wnt�y)}zd|��|d<|WYd	}~Sd	}~ww|d�dddd
�i�n>|�su|�rutd
�siz
td|dd�Wnt�yh}zd|��|d<|WYd	}~Sd	}~ww|d�dddd
�i�|�s{|�rF|�pg}z
td|dd�}Wnt�y�}zd|��|d<|WYd	}~Sd	}~wwt|�t|�}g}|D]4}td
�s�z
td||ddd�W�q�t�y�}zd|��|d<|WYd	}~Sd	}~ww�q�|�r)t|�t|�}|D]3}td
�s'ztd ||dd�W�q�t�y&}zd|��|d<|WYd	}~Sd	}~ww�q�|�s/|�rF|�s:t|t|�B�}|d�d!||d
�i�|�sL|�r=|�pPg}z
td"|dd�}Wnt�yw}zd|��|d<|WYd	}~Sd	}~wwd#d$�|D�}d%d$�|D�}t|�t|�} g}!| D];}"td
�s�ztd&||"j	|"j
|"j|"jddd�W�q�t�y�}zd|��|d<|WYd	}~Sd	}~ww�q�|�rt|�t|�}!|!D]:}"td
�sztd'||"j	|"j
|"j|"jdd�W�q�t�y}zd|��|d<|WYd	}~Sd	}~ww�q�| �s|!�r=|�s't| t|�B�}|d�d(d)d$�|D�d*d$�|D�d
�i�|	�sC|
�r
|	�pGg}	z
td+|dd�}#Wnt�yn}zd|��|d<|WYd	}~Sd	}~wwt|	�t|#�}$g}%|$D]3}&td
�s�ztd,|&|dd�W�q{t�y�}zd|��|d<|WYd	}~Sd	}~ww�q{|
�r�t|#�t|	�}%|%D]3}'td
�s�ztd-|'|dd�W�q�t�y�}zd|��|d<|WYd	}~Sd	}~ww�q�|$�s�|%�r
|
�st|$t|#�B�}	|d�d.|#|	d
�i�|�s|�r�|�pg}z
td/|dd�}(Wnt�y>}zd|��|d<|WYd	}~Sd	}~wwt|�t|(�})g}*|)D]3}+td
�s}ztd0||+dd�W�qKt�y|}zd|��|d<|WYd	}~Sd	}~ww�qK|�r�t|(�t|�}*|*D]3}+td
�s�ztd1||+dd�W�q�t�y�}zd|��|d<|WYd	}~Sd	}~ww�q�|)�s�|*�r�|�s�t|)t|(�B�}|d�d2|(|d
�i�|
�s�|�r�|
�p�g}
z
td3|dd�},Wnt�y}zd|��|d<|WYd	}~Sd	}~wwt|
�t|,�}-g}.|-D]3}/td
�sMztd4||/dd�W�qt�yL}zd|��|d<|WYd	}~Sd	}~ww�q|�r�t|,�t|
�}.|.D]3}/td
�s�ztd5||/dd�W�q\t�y�}zd|��|d<|WYd	}~Sd	}~ww�q\|-�s�|.�r�|�s�t|-t|,�B�}
|d�d6|,|
d
�i�|�s�|�r}|�p�g}z
td7|dd�}0Wnt�y�}zd|��|d<|WYd	}~Sd	}~wwt|�t|0�}1g}2|1D]3}3td
�sztd8||3dd�W�q�t�y}zd|��|d<|WYd	}~Sd	}~ww�q�|�r`t|0�t|�}2|2D]3}3td
�s^ztd9||3dd�W�q,t�y]}zd|��|d<|WYd	}~Sd	}~ww�q,|1�sf|2�r}|�sqt|1t|0�B�}|d�d:|0|d
�i�|dik�r�d|d;<d<|�d=�|d<|Std
�r�d	|d;<tt
_g}4|4�d>|�d?��|4�t
�|d����d@�|4�|d<i|d<|Sd|d;<d<|�dA�|d<|S)Bz0
    Ensure a zone has specific attributes.
    Fr!rBzfirewalld.get_zonesT)�	permanentrErDNrFzfirewalld.new_zoner+rGzfirewalld.list_icmp_blockzfirewalld.get_icmp_typesz%s is an invalid ICMP typezfirewalld.block_icmpzfirewalld.allow_icmpZ
icmp_typeszfirewalld.default_zonezfirewalld.set_default_zoner2zfirewalld.get_masqueradezfirewalld.add_masquerader3zMasquerading successfully set.zfirewalld.remove_masqueradez#Masquerading successfully disabled.zfirewalld.list_portszfirewalld.add_port)rXZforce_masqueradezfirewalld.remove_portr4zfirewalld.list_port_fwdcSsg|]}t|��qSr)r%��.0�fwdrrr
�
<listcomp>=�z_present.<locals>.<listcomp>cSs,g|]}t|d|d|d|dd��qS)rrrrr)rrYrrr
r\>s���zfirewalld.add_port_fwdzfirewalld.remove_port_fwdr6cS�g|]}|���qSr�rrYrrr
r\tr]cSr^rr_rYrrr
r\ur]zfirewalld.list_serviceszfirewalld.add_servicezfirewalld.remove_servicer8zfirewalld.get_interfaceszfirewalld.add_interfacezfirewalld.remove_interfacer:zfirewalld.get_sourceszfirewalld.add_sourcezfirewalld.remove_sourcer<zfirewalld.get_rich_ruleszfirewalld.add_rich_rulezfirewalld.remove_rich_ruler>rCrKrLrMz' will change:�
rN)r.rrPrQrO�log�error�remove�listrrr	r
r�append�output�rstrip�join)5r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@ZzonesrSZnew_icmp_typesZold_icmp_typesZ_current_icmp_blocksZ_valid_icmp_typesZ	icmp_typeZdefault_zoneZmasquerade_retrRrTrUrVZ_current_port_fwdZnew_port_fwdZold_port_fwdr[Z_current_servicesZnew_servicesZold_servicesZnew_serviceZold_serviceZ_current_interfacesZnew_interfacesZold_interfacesZ	interfaceZ_current_sourcesZnew_sourcesZold_sources�sourceZ_current_rich_rulesZnew_rich_rulesZold_rich_rulesZ	rich_rulerDrrr
r-{s:����

�����

����	
����	���

����
���
���
��
����	
���

����

�	���
�������	

���
���
����	�

���
���
����	�
��
���
����	�

���
���
����	�
r-)NFNFNFNFNFNFNFNF)NN)NFNFNFNFNNNFNFNF)r�loggingZsalt.utils.pathr&Zsalt.exceptionsrZsalt.outputr�	getLoggerrrarr%r*rArWr-rrrr
�<module>sXh
,
�
e]�