
    Vhq.                        d dl mZmZmZ d dlmZ d dlmZmZ d dl	m
Z
 eZdZdZdZdZ	 d dlZej&                  j(                  Zd dlmZ d d	lmZ d d
lmZ dZ	  e       Zej5                           G d de#      Z$y# eef$ r> dZ	 d dlmZ  e       Zn# e$ r d dlm Z   e d      ZY nw xY wejC                          Y Pw xY w# e"$ r Y [w xY w)    )absolute_importdivisionprint_function)LooseVersion)respawn_moduleHAS_RESPAWN_UTIL)missing_required_libNFT)FirewallClient)FirewallClientZoneSettings)FirewallError)Firewall_test)Firewall)offlinec                   h    e Zd ZdZ	 	 ddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zed        Zy)FirewallTransactionzn
    FirewallTransaction

    This is the base class for all firewalld transactions we might want to have
    Nc	                 V   || _         t        | _        || _        |r|| _        n9t        rt        j                         | _        nt        j                         | _        || _        || _        || _	        t        | _        |xs dg| _
        |xs dg| _        g | _        d| _        d| _        y)aq  
        initializer the transaction

        :module:          AnsibleModule, instance of AnsibleModule
        :action_args:     tuple, args to pass for the action to take place
        :zone:            str,  firewall zone
        :desired_state:   str,  the desired state (enabled, disabled, etc)
        :permanent:       bool, action should be permanent
        :immediate:       bool, action should take place immediately
        :enabled_values:  str[], acceptable values for enabling something (default: enabled)
        :disabled_values: str[], acceptable values for disabling something (default: disabled)
        enableddisabledN)modulefwaction_argszone
fw_offlineget_default_zonegetDefaultZonedesired_state	permanent	immediateenabled_valuesdisabled_valuesmsgsenabled_msgdisabled_msg)	selfr   r   r   r   r   r   r   r    s	            p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/module_utils/firewalld.py__init__zFirewallTransaction.__init__;   s      &DI//1	--/	*""$,;.>:, 	       c           	      n   	  || S # t         $ r}dd|z  v r| j                  j                  d       t        | j                        dkD  r<| j                  j                  d|ddj                  | j                               n$| j                  j                  d	|z         Y d
}~y
Y d
}~y
d
}~ww xY w)z
        Function to wrap calls to make actions on firewalld in try/except
        logic and emit (hopefully) useful error messages
        INVALID_SERVICEz%szfServices are defined by port/tcp relationship and named as they are in /etc/services (on most systems)r   zERROR: Exception caught:  z, msgzERROR: Exception caught: %sN)	Exceptionr!   appendlenr   	fail_jsonjoin)r$   action_funcaction_func_argses       r%   action_handlerz"FirewallTransaction.action_handlerj   s    	M 011 	M !D1H,		    "J  K499~!%%<=tyy?ST &  %%*G!*K%LL		Ms    	B4BB//B4c                 |   | j                   rj| j                  j                  j                  | j                        }t        t        | j                  j                  j                  |                  }||fS | j                  j                         j                  | j                        }|j                         }||fS N)
r   r   configget_zoner   r   listget_zone_configgetZoneByNamegetSettingsr$   fw_zonefw_settingss      r%   get_fw_zone_settingsz(FirewallTransaction.get_fw_zone_settings   s    ??ggnn--dii8G4TWW^^33G<=K %% ggnn&44TYY?G!--/K%%r'   c                     | j                   r1| j                  j                  j                  ||j                         y |j                  |       y r7   )r   r   r8   set_zone_configsettingsupdater>   s      r%   update_fw_settingsz&FirewallTransaction.update_fw_settings   s3    ??GGNN**7K4H4HINN;'r'   c                     t         r7   NotImplementedErrorr$   s    r%   get_enabled_immediatez)FirewallTransaction.get_enabled_immediate       !!r'   c                     t         r7   rH   rJ   s    r%   get_enabled_permanentz)FirewallTransaction.get_enabled_permanent   rL   r'   c                     t         r7   rH   rJ   s    r%   set_enabled_immediatez)FirewallTransaction.set_enabled_immediate   rL   r'   c                     t         r7   rH   rJ   s    r%   set_enabled_permanentz)FirewallTransaction.set_enabled_permanent   rL   r'   c                     t         r7   rH   rJ   s    r%   set_disabled_immediatez*FirewallTransaction.set_disabled_immediate   rL   r'   c                     t         r7   rH   rJ   s    r%   set_disabled_permanentz*FirewallTransaction.set_disabled_permanent   rL   r'   c                 (   d| _         | j                  rO| j                  rB| j                  | j                  | j
                        }| j                  | j                  | j
                        }| j                  j                  d       | j                  | j                  v r|r|s2| j                  j                  r| j                  j                  d       |s-| j                  | j                  | j
                         d| _         |s-| j                  | j                  | j
                         d| _         | j                   r| j                   r| j                  j                  | j                          n| j                  | j"                  v rm|s|r2| j                  j                  r| j                  j                  d       |r-| j                  | j$                  | j
                         d| _         |r-| j                  | j&                  | j
                         d| _         | j                   r| j(                  r| j                  j                  | j(                         n| j                  r| j                  s| j                  | j                  | j
                        }| j                  j                  d       | j                  | j                  v r|s_| j                  j                  r| j                  j                  d       | j                  | j                  | j
                         d| _         | j                   r| j                   r| j                  j                  | j                          n| j                  | j"                  v rj|r_| j                  j                  r| j                  j                  d       | j                  | j$                  | j
                         d| _         | j                   r| j(                  r| j                  j                  | j(                         n| j                  r| j                  s| j                  | j                  | j
                        }| j                  j                  d       | j                  | j                  v r|s_| j                  j                  r| j                  j                  d       | j                  | j                  | j
                         d| _         | j                   r| j                   r| j                  j                  | j                          n| j                  | j"                  v r|r_| j                  j                  r| j                  j                  d       | j                  | j&                  | j
                         d| _         | j                   r1| j(                  r%| j                  j                  | j(                         | j                   | j                  fS )z
        run

        This function contains the "transaction logic" where as all operations
        follow a similar pattern in order to perform their action but simply
        call different functions to carry that action out.
        Fz0Permanent and Non-Permanent(immediate) operationT)changedzPermanent operationzNon-permanent operation)rX   r   r   r5   rN   r   rK   r!   r.   r   r   r   
check_mode	exit_jsonrR   rP   r"   r    rV   rT   r#   )r$   is_enabled_permanentis_enabled_immediate
is_enableds       r%   runzFirewallTransaction.run   s    >>dnn#'#6#6**  $  $(#6#6**  $  IIOP!!T%8%88+3G{{----d-;+''22(( $(DL+''22(( $(DL<<D$4$4II$$T%5%56##t';';;'+?{{----d-;'''33(( $(DL'''33(( $(DL<<D$5$5II$$T%6%67^^DNN,,**  J II23!!T%8%88!{{----d-;''22(( $(DL<<D$4$4II$$T%5%56##t';';;{{----d-;''33(( $(DL<<D$5$5II$$T%6%67^^DNN,,**  J II67!!T%8%88!{{----d-;''22(( $(DL<<D$4$4II$$T%5%56##t';';;{{----d-;''33(( $(DL<<D$5$5II$$T%6%67dii((r'   c                 <   t         rGt        rAt        t               t        d      k  r| j                  dj	                  t                      nqt         rkt        set        t               t        d      k  r%| j                  dj	                  t                      	 t
        j                  du r| j                  d       t        r0t        rt        d	       | j                  t        d	      d
z          yy# t        $ r | j                  dt         z         Y [w xY w)zj
        Perform sanity checking, version checks, etc

        :module:    AnsibleModule instance
        z0.3.9zRunsupported version of firewalld, offline operations require >= 0.3.9 - found: {0}r+   z0.2.11zAunsupported version of firewalld, requires >= 0.2.11 - found: {0}Fz;firewalld service must be running, or try with offline=truezfirewalld connection can't be established,                        installed version (%s) likely too old. Requires firewalld >= 0.2.11firewallzJ. Version 0.2.11 or newer required (0.3.9 or newer for offline operations)N)
FW_VERSIONr   r   r0   formatr   	connectedAttributeErrorimport_failurer   r   r	   )r   s    r%   sanity_checkz FirewallTransaction.sanity_check$  s    *J',w*??  %y  &A  &A  BL  &M   N
J',x*@@  %h%o%opz%{ |k<<5($$)f$g
 z*(4  8D  D   	 " k   &]_i&j  kks   $C6 6"DD) NNFFNN)__name__
__module____qualname____doc__r&   r5   rA   rF   rK   rN   rP   rR   rT   rV   r^   staticmethodrf   rg   r'   r%   r   r   4   s`     IMX\*!^M,
&(""""""~)@  r'   r   )%
__future__r   r   r   >ansible_collections.ansible.posix.plugins.module_utils.versionr   ?ansible_collections.ansible.posix.plugins.module_utils._respawnr   r   ansible.module_utils.basicr	   type__metaclass__ra   r   r   re   firewall.configr`   r8   VERSIONfirewall.clientr
   r   firewall.errorsr   r   rd   firewall.core.fw_testr   ModuleNotFoundErrorfirewall.core.fwr   startImportErrorobjectr   rg   r'   r%   <module>r}      s    A @ W l ; 
	
	((J.:-N
.N& N+ M* 
	(;B# 	(1$'B	(
 	
!"  		sS   .C A< <	CBCB+(C*B++C>C  CC CC