
    Vh6                     L   d dl mZmZmZ eZdZdZdZd dl	m
Z
mZ d dlmZ d dlmZmZ d dlmZ 	 d d	lZd
Z	 d d	lmZ d d	lmZ d
Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/e0dk(  r e/        y	y	# e$ r dZY _w xY w# e$ r dZY ]w xY w)     )absolute_importdivisionprint_functionaf  
---
module: firewalld_info
short_description: Gather information about firewalld
description:
    - This module gathers information about firewalld rules.
options:
    active_zones:
        description: Gather information about active zones.
        type: bool
        default: false
    zones:
        description:
            - Gather information about specific zones.
            - If only works if O(active_zones=false).
        required: false
        type: list
        elements: str
requirements:
    - firewalld >= 0.2.11
    - python-firewall
    - python-dbus
author:
    - Hideki Saito (@saito-hideki)
a  
- name: Gather information about active zones
  ansible.posix.firewalld_info:
    active_zones: true
  register: result

- name: Print default zone for debugging
  ansible.builtin.debug:
    var: result.firewalld_info.default_zone

- name: Gather information about specific zones
  ansible.posix.firewalld_info:
    zones:
      - public
      - external
      - internal
  register: result
a  
active_zones:
    description:
      - Gather active zones only if turn it C(true).
    returned: success
    type: bool
    sample: false
collected_zones:
    description:
      - A list of collected zones.
    returned: success
    type: list
    sample: [external, internal]
undefined_zones:
    description:
      - A list of undefined zones in C(zones) option.
      - C(undefined_zones) will be ignored for gathering process.
    returned: success
    type: list
    sample: [foo, bar]
firewalld_info:
    description:
      - Returns various information about firewalld configuration.
    returned: success
    type: complex
    contains:
        version:
            description:
              - The version information of firewalld.
            returned: success
            type: str
            sample: 0.8.2
        default_zone:
            description:
              - The zone name of default zone.
            returned: success
            type: str
            sample: public
        zones:
            description:
              - A dict of zones to gather information.
            returned: success
            type: complex
            contains:
                zone:
                    description:
                      - The zone name registered in firewalld.
                    returned: success
                    type: complex
                    sample: external
                    contains:
                        target:
                            description:
                              - A list of services in the zone.
                            returned: success
                            type: str
                            sample: ACCEPT
                        icmp_block_inversion:
                            description:
                              - The ICMP block inversion to block
                                all ICMP requests.
                            returned: success
                            type: bool
                            sample: false
                        interfaces:
                            description:
                              - A list of network interfaces.
                            returned: success
                            type: list
                            sample:
                              - 'eth0'
                              - 'eth1'
                        sources:
                            description:
                              - A list of source network address.
                            returned: success
                            type: list
                            sample:
                              - '172.16.30.0/24'
                              - '172.16.31.0/24'
                        services:
                            description:
                              - A list of network services.
                            returned: success
                            type: list
                            sample:
                              - 'dhcp'
                              - 'dns'
                              - 'ssh'
                        ports:
                            description:
                              - A list of network port with protocol.
                            returned: success
                            type: list
                            sample:
                              - - "22"
                                - "tcp"
                              - - "80"
                                - "tcp"
                        protocols:
                            description:
                              - A list of network protocol.
                            returned: success
                            type: list
                            sample:
                              - "icmp"
                              - "ipv6-icmp"
                        forward:
                            description:
                              - The network interface forwarding.
                              - This parameter supports on python-firewall
                                0.9.0(or later) and is not collected in earlier
                                versions.
                            returned: success
                            type: bool
                            sample: false
                        masquerade:
                            description:
                              - The network interface masquerading.
                            returned: success
                            type: bool
                            sample: false
                        forward_ports:
                            description:
                              - A list of forwarding port pair with protocol.
                            returned: success
                            type: list
                            sample:
                              - "icmp"
                              - "ipv6-icmp"
                        source_ports:
                            description:
                              - A list of network source port with protocol.
                            returned: success
                            type: list
                            sample:
                              - - "30000"
                                - "tcp"
                              - - "30001"
                                - "tcp"
                        icmp_blocks:
                            description:
                              - A list of blocking icmp protocol.
                            returned: success
                            type: list
                            sample:
                              - "echo-request"
                        rich_rules:
                            description:
                              - A list of rich language rule.
                            returned: success
                            type: list
                            sample:
                              - "rule protocol value=\"icmp\" reject"
                              - "rule priority=\"32767\" reject"
)AnsibleModulemissing_required_lib)	to_native)respawn_moduleHAS_RESPAWN_UTIL)StrictVersionNTFc                  "    t         j                  S N)	fw_configVERSION     p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/modules/firewalld_info.pyget_versionr      s    r   c                 >    | j                         j                         S r   )getActiveZoneskeysclients    r   get_active_zonesr      s      "''))r   c                 "    | j                         S r   )getZonesr   s    r   get_all_zonesr      s    ??r   c                 "    | j                         S r   )getDefaultZoner   s    r   get_default_zoner      s      ""r   c                 $    | j                  |      S r   )getZoneSettings)r   zones     r   get_zone_settingsr#      s    !!$''r   c                 "    | j                         S r   )	getTargetzone_settingss    r   get_zone_targetr(      s    ""$$r   c                 "    | j                         S r   )getIcmpBlockInversionr&   s    r   get_zone_icmp_block_inversionr+      s    ..00r   c                 "    | j                         S r   )getInterfacesr&   s    r   get_zone_interfacesr.         &&((r   c                 "    | j                         S r   )
getSourcesr&   s    r   get_zone_sourcesr2         ##%%r   c                 "    | j                         S r   )getServicesr&   s    r   get_zone_servicesr6     s    $$&&r   c                 "    | j                         S r   )getPortsr&   s    r   get_zone_portsr9     s    !!##r   c                 "    | j                         S r   )getProtocolsr&   s    r   get_zone_protocolsr<         %%''r   c                 "    | j                         S r   )
getForwardr&   s    r   get_zone_forwardr@     r3   r   c                 "    | j                         S r   )getMasquerader&   s    r   get_zone_masqueraderC     r/   r   c                 "    | j                         S r   )getForwardPortsr&   s    r   get_zone_forward_portsrF   !  s    ((**r   c                 "    | j                         S r   )getSourcePortsr&   s    r   get_zone_source_portsrI   %  s    ''))r   c                 "    | j                         S r   )getIcmpBlocksr&   s    r   get_zone_icmp_blocksrL   )  r/   r   c                 "    | j                         S r   )getRichRulesr&   s    r   get_zone_rich_rulesrO   -  r=   r   c                     t        t        ddd      t        ddd            } t        | d	      }t               }t        d|j                  d
   t               t               t                     }t        st
        st        rt        d       t        s|j                  t        d             t
        s|j                  t        d             t               }	 t        j                         }t               |d<   t        |      |d<   t               }t               }t               }|j                  d
   rt        |      }n|j                  d   rt        |      }	|j                  d   }
t        t!        |
      t!        |	      z        }t        t!        |
      t!        |      z
        }|r/|j#                  ddj%                  |      z         nt        |      }|D ]  }t               }t'        ||      }t)        |      |d<   t+        |      |d<   t-        |      |d<   t/        |      |d<   t1        |      |d<   t3        |      |d<   t5        |      |d<   t7        |      |d<   t9        |      |d<   t;        |      |d<   t=        |      |d<   t?        |      |d <   tA        |d         tA        d!      k\  rtC        |      |d"<   |||<    ||d<   |d%<   |d&<   ||d'<   ||d(<    |jN                  d)i | y # tD        $ r(}|j                  d#tG        |      z         Y d }~Sd }~wtH        jJ                  jL                  $ r(}|j                  d$tG        |      z         Y d }~d }~ww xY w)*NFbool)requiredtypedefaultliststr)rR   rS   elements)active_zoneszonesT)argument_specsupports_check_moderX   )changedrX   collected_zonesundefined_zoneswarningsfirewallzpython-dbus)msgzpython-firewallversiondefault_zonerY   zBPlease note: zone:(%s) have been ignored in the gathering process.,targeticmp_block_inversion
interfacessourcesservicesports	protocols
masqueradeforward_portssource_portsicmp_blocks
rich_rulesz0.9.0forwardzzfirewalld probably not be running, Or the following method is not supported with your python-firewall version. (Error: %s)zdUnable to gather firewalld settings. You may need to run as the root user or use become. (Error: %s)r]   r^   firewalld_infor_   r   )(dictr   paramsrU   HAS_DBUSHAS_FIREWALLDr
   r	   	fail_jsonr   	fw_clientFirewallClientr   r   r   r   setappendjoinr#   r(   r+   r.   r2   r6   r9   r<   rC   rF   rI   rL   rO   r   r@   AttributeErrorr   dbus
exceptionsDBusException	exit_json)module_argsmodulerr   resultwarnr   
zones_infocollect_zonesignore_zones	all_zonesspecified_zonesr"   	zone_infor'   es                  r   mainr   1  ss   5vuEE?K
 ! F
 VN]]>2F M.> 	z"1-@A12CDE 6D6J))+ %0My!)9&)A~& V
v==(,V4M]]7#%f-I$mmG4O _!5I!FGMO 4s=7I IJLX[^[c[cdp[qqs *&1M! 	)D I-fd;M"1-"@Ih0Mm0\I,-&9-&HIl##3M#BIi $5m$DIj!!/!>Ig%7%FIk"&9-&HIl#)?)NIo&(=m(LIn%';M'JIm$&9-&HIl# ^I67=;QQ'7'F	)$(Jt-	). #-w !.F ,F-FF:Fv  q `clmncop 	q 	q??(( J 9;DQ<H 	J 	JJs%   G9K< <	M0L(( M0M++M0__main__)1
__future__r   r   r   rS   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   r   ansible.module_utils._textr   ?ansible_collections.ansible.posix.plugins.module_utils._respawnr	   r
   >ansible_collections.ansible.posix.plugins.module_utils.versionr   r~   ru   ImportErrorfirewall.clientr   rx   firewall.configconfigr   rv   r   r   r   r   r#   r(   r+   r.   r2   r6   r9   r<   r@   rC   rF   rI   rL   rO   r   __name__r   r   r   <module>r      s    C B4&[
z K 0 l XH''M
*#(%1)&'$(
&)+*)(`F zF o  H  Ms"   B B BBB#"B#