
    VhvF                     .   d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lZ	 d d	lZd d	lZd d	lZd d	lZd d	lZd d
lmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- dZ. e       Z0d Z1 G d de      Z2y	# e/$ r dZ.Y w xY w)    )absolute_importdivisionprint_functiona_  
name: dig
author: Jan-Piet Mens (@jpmens) <jpmens(at)gmail.com>
short_description: query DNS using the dnspython library
requirements:
  - dnspython (python library, http://www.dnspython.org/)
description:
  - The dig lookup runs queries against DNS servers to retrieve DNS records for a specific name (FQDN - fully qualified domain
    name). It is possible to lookup any DNS record in this manner.
  - There is a couple of different syntaxes that can be used to specify what record should be retrieved, and for which name.
    It is also possible to explicitly specify the DNS server(s) to use for lookups.
  - In its simplest form, the dig lookup plugin can be used to retrieve an IPv4 address (DNS A record) associated with FQDN.
  - In addition to (default) A record, it is also possible to specify a different record type that should be queried. This
    can be done by either passing-in additional parameter of format qtype=TYPE to the dig lookup, or by appending /TYPE to
    the FQDN being queried.
  - If multiple values are associated with the requested record, the results will be returned as a comma-separated list. In
    such cases you may want to pass option C(wantlist=true) to the lookup call, or alternatively use C(query) instead of C(lookup),
    which will result in the record values being returned as a list over which you can iterate later on.
  - By default, the lookup will rely on system-wide configured DNS servers for performing the query. It is also possible to
    explicitly specify DNS servers to query using the @DNS_SERVER_1,DNS_SERVER_2,...,DNS_SERVER_N notation. This needs to
    be passed-in as an additional parameter to the lookup.
options:
  _terms:
    description: Domain(s) to query.
    type: list
    elements: str
  qtype:
    description:
      - Record type to query.
      - V(DLV) has been removed in community.general 6.0.0.
      - V(CAA) has been added in community.general 6.3.0.
    type: str
    default: 'A'
    choices:
      - A
      - ALL
      - AAAA
      - CAA
      - CNAME
      - DNAME
      - DNSKEY
      - DS
      - HINFO
      - LOC
      - MX
      - NAPTR
      - NS
      - NSEC3PARAM
      - PTR
      - RP
      - RRSIG
      - SOA
      - SPF
      - SRV
      - SSHFP
      - TLSA
      - TXT
  flat:
    description: If 0 each record is returned as a dictionary, otherwise a string.
    type: int
    default: 1
  retry_servfail:
    description: Retry a nameserver if it returns SERVFAIL.
    default: false
    type: bool
    version_added: 3.6.0
  fail_on_error:
    description:
      - Abort execution on lookup errors.
      - The default for this option will likely change to V(true) in the future. The current default, V(false), is used for
        backwards compatibility, and will result in empty strings or the string V(NXDOMAIN) in the result in case of errors.
    default: false
    type: bool
    version_added: 5.4.0
  real_empty:
    description:
      - Return empty result without empty strings, and return empty list instead of V(NXDOMAIN).
      - The default for this option will likely change to V(true) in the future.
      - This option will be forced to V(true) if multiple domains to be queried are specified.
    default: false
    type: bool
    version_added: 6.0.0
  class:
    description:
      - Class.
    type: str
    default: 'IN'
  tcp:
    description: Use TCP to lookup DNS records.
    default: false
    type: bool
    version_added: 7.5.0
  port:
    description: Use port as target port when looking up DNS records.
    default: 53
    type: int
    version_added: 9.5.0
notes:
  - V(ALL) is not a record in itself, merely the listed fields are available for any record results you retrieve in the form of
    a dictionary.
  - While the plugin supports anything which C(dnspython) supports out of the box, only a subset can be converted
    into a dictionary.
  - If you need to obtain the AAAA record (IPv6 address), you must specify the record type explicitly. Syntax for specifying
    the record type is shown in the examples below.
  - The trailing dot in most of the examples listed is purely optional, but is specified for completeness/correctness sake.
a  
- name: Simple A record (IPV4 address) lookup for example.com
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.dig', 'example.com.')}}"

- name: "The TXT record for example.org."
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.dig', 'example.org.', qtype='TXT') }}"

- name: "The TXT record for example.org, alternative syntax."
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.dig', 'example.org./TXT') }}"

- name: use in a loop
  ansible.builtin.debug:
    msg: "MX record for gmail.com {{ item }}"
  with_items: "{{ lookup('community.general.dig', 'gmail.com./MX', wantlist=true) }}"

- name: Lookup multiple names at once
  ansible.builtin.debug:
    msg: "A record found {{ item }}"
  loop: "{{ query('community.general.dig', 'example.org.', 'example.com.', 'gmail.com.') }}"

- name: Lookup multiple names at once (from list variable)
  ansible.builtin.debug:
    msg: "A record found {{ item }}"
  loop: "{{ query('community.general.dig', *hosts) }}"
  vars:
    hosts:
      - example.org.
      - example.com.
      - gmail.com.

- ansible.builtin.debug:
    msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '192.0.2.5/PTR') }}"
- ansible.builtin.debug:
    msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa./PTR') }}"
- ansible.builtin.debug:
    msg: "Reverse DNS for 192.0.2.5 is {{ lookup('community.general.dig', '5.2.0.192.in-addr.arpa.', qtype='PTR') }}"
- ansible.builtin.debug:
    msg: "Querying 198.51.100.23 for IPv4 address for example.com. produces {{ lookup('dig', 'example.com', '@198.51.100.23') }}"

- ansible.builtin.debug:
    msg: "XMPP service for gmail.com. is available at {{ item.target }} on port {{ item.port }}"
  with_items: "{{ lookup('community.general.dig', '_xmpp-server._tcp.gmail.com./SRV', flat=0, wantlist=true) }}"

- name: Retry nameservers that return SERVFAIL
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.dig', 'example.org./A', retry_servfail=true) }}"
a  
_list:
  description:
    - List of composed strings or of dictionaries, with fields depending
      on query type.
  type: list
  elements: raw
  contains:
    ALL:
      description:
        - C(owner), C(ttl), C(type).
    A:
      description:
        - C(address).
    AAAA:
      description:
        - C(address).
    CAA:
      description:
        - C(flags).
        - C(tag).
        - C(value).
      version_added: 6.3.0
    CNAME:
      description:
        - C(target).
    DNAME:
      description:
        - C(target).
    DNSKEY:
      description:
        - C(flags), C(algorithm), C(protocol), C(key).
    DS:
      description:
        - C(algorithm), C(digest_type), C(key_tag), C(digest).
    HINFO:
      description:
        - C(cpu), C(os).
    LOC:
      description:
        - C(latitude), C(longitude), C(altitude), C(size), C(horizontal_precision), C(vertical_precision).
    MX:
      description:
        - C(preference), C(exchange).
    NAPTR:
      description:
        - C(order), C(preference), C(flags), C(service), C(regexp), C(replacement).
    NS:
      description:
        - C(target).
    NSEC3PARAM:
      description:
        - C(algorithm), C(flags), C(iterations), C(salt).
    PTR:
      description:
        - C(target).
    RP:
      description:
        - C(mbox), C(txt).
    SOA:
      description:
        - C(mname), C(rname), C(serial), C(refresh), C(retry), C(expire), C(minimum).
    SPF:
      description:
        - C(strings).
    SRV:
      description:
        - C(priority), C(weight), C(port), C(target).
    SSHFP:
      description:
        - C(algorithm), C(fp_type), C(fingerprint).
    TLSA:
      description:
        - C(usage), C(selector), C(mtype), C(cert).
    TXT:
      description:
        - C(strings).
)AnsibleError)
LookupBase)boolean)DisplayN)AAAAACAACNAMEDNAMEDNSKEYDSHINFOLOCMXNAPTRNS
NSEC3PARAMPTRRPSOASPFSRVSSHFPTLSATXTTFc           	         i t         dgt        dgt        g dt        dgt        dgt
        g dt        g dt        ddgt        g dt        d	d
gt        g dt        dgt        g dt        dgt        ddgt        g dt         dgt"        g dt$        g dt&        g dt(        dgi}i }| j*                  |v r3|| j*                     }|D ]  }| j-                  |      }t/        |t0        j2                  j4                        r)t0        j2                  j4                  j7                  |      }| j*                  t        k(  r>|dk(  r9t0        j8                  j;                  | j<                        j?                  dd      }| j*                  t
        k(  r|dk(  rtA        |      }| j*                  t
        k(  r>|dk(  r9t0        j8                  jC                  | jD                        j?                  dd      }| j*                  t        k(  r>|dk(  r9t0        j8                  j;                  | jF                        j?                  dd      }| j*                  t$        k(  r>|dk(  r9t0        j8                  j;                  | jH                        j?                  dd      }| j*                  t&        k(  r>|dk(  r9t0        j8                  j;                  | jJ                        j?                  dd      }|||<   ! |S )a   While the 'dig' lookup plugin supports anything which dnspython supports
        out of the box, the following supported_types list describes which
        DNS query types we can convert to a dict.

        Note: adding support for RRSIG is hard work. :)
    address)flagstagvaluetarget)r!   	algorithmprotocolkey)r%   digest_typekey_tagdigestcpuos)latitude	longitudealtitudesizehorizontal_precisionvertical_precision
preferenceexchange)orderr3   r!   serviceregexpreplacement)r%   r!   
iterationssaltmboxtxt)mnamernameserialrefreshretryexpireminimumstrings)priorityweightportr$   )r%   fp_typefingerprint)usageselectormtypecertr*     r%   r'   r:   rI   rM   )&r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rdtype__getattribute__
isinstancednsnameNameto_textrdata_hexifyr*   replaceint
_base64ifyr'   r:   rI   rM   )rW   supported_typesrdfieldsfvals         h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/dig.pymake_rdata_dictrb     s   	I;yk 	& 	z	
 	z 	9 	= 	t} 	h 	\:& 	S 	XJ 	@ 	hZ 	VUO" 	R#$ 	i[%& 	564i[-O2 
B||& . 	A((+C#sxx}}-hhmm++C0||r!a8mii''5==c2F||v%!{*:#h||v%!u*ii**5995==c2F||z)a6kii''

3;;CD||u$m);ii''(9(9:BB3K||t#Vii''

3;;CDBqE'	* I    c                       e Zd ZddZy)LookupModuleNc                    t         du rt        d      | j                  ||       t        j                  j                  d      }d}|j                  dt        j                  j                  |       g }g }| j                  d	      }| j                  d
      }	| j                  d      }
| j                  d      }| j                  d      }| j                  d      }	 t        j                  j                  | j                  d            }| j                  d      |_        |D ]E  }|j                  d      rC|dd j                  d      }|D ])  }	 t!        j"                  |       |j%                  |       + Xd|v r	 |j                  dd      \  }}d	k(  rj+                         }n|d
k(  rt-              }	nn|dk(  r!	 t        j                  j                        }nH|dk(  rt/              |_        n2|dk(  rt/              }
n!|dk(  rt/              }n|dk(  rt/              }	d|v r(	 |j                  d      \  }}|j%                  |       5|j%                  |       H |r||_        t3        |      dkD  r||_        |j+                         dk(  rJg }|D ]A  }	 t        j6                  j9                  |      }|j%                  |j;                                C |}t3        |      dkD  rd}g }|D ]  }	 |j'                  ||||      }|D ]  }|j;                         }|j+                         dk(  r|dd }|	r|j%                  |       ?	 tA        |      }|jB                  j;                         |d<   t        jD                  j;                  |jF                        |d<   |jH                  jJ                  |d <   t        j                  j;                  |jL                        |d<   |j%                  |        
 |S # t        $ r}t        d|       d}~ww xY w# t        $ rb 	 t        j                  j'                  |      d   j(                  }|j%                  |       n# t        $ r}t        d|       d}~ww xY wY Cw xY w# t        $ r Y w xY w# t        $ r}t        d|       d}~ww xY w# t        $ r |j%                  |       Y w xY w# t        j<                  j>                  $ r Y dt        $ r}t        d|       d}~ww xY w# t        $ r5}|
rt        d!|       |j%                  tO        |             Y d}~Ed}~ww xY w# t        j                  jP                  $ r.}|
rt        d!|       |s|j%                  d"       Y d}~d}~wt        j                  jR                  t        j                  jT                  t        j                  jV                  f$ r.}|
rt        d!|       |s|j%                  d#       Y d}~+d}~wt        j<                  jX                  $ r}t        d$|       d}~ww xY w)%a  
        terms contains a string with things to `dig' for. We support the
        following formats:
            example.com                                     # A record
            example.com  qtype=A                            # same
            example.com/TXT                                 # specific qtype
            example.com  qtype=txt                          # same
            192.0.2.23/PTR                                 # reverse PTR
              ^^ shortcut for 23.2.0.192.in-addr.arpa/PTR
            example.net/AAAA  @nameserver                   # query specified server
                               ^^^ can be comma-sep list of names/addresses

            ... flat=0                                      # returns a dict; default is 1 == string
        FzNThe dig lookup requires the python 'dnspython' library and it is not installed)var_optionsdirectT)	configurei   r   )	ednsflagspayloadqtypeflatfail_on_error
real_emptytcprG   classzdns lookup illegal CLASS: Nretry_servfail@   ,zdns lookup NS: =/r   z$dns.reversename unhandled exception )rdclassrp   r   ownertypettlzLookup failed: NXDOMAINrO   z!dns.resolver unhandled exception )-HAVE_DNSr   set_optionsrS   resolverResolveruse_ednsr!   DO
get_option
rdataclass	from_text	Exceptionrr   
startswithsplitsocket	inet_atonappendqueryr    upperrZ   r   rG   lennameserversreversenamefrom_addressrV   	exceptionSyntaxErrorrb   canonical_name	rdatatyperP   rrsetr|   rx   strr}   NoAnswerTimeoutNoNameserversDNSException)selfterms	variableskwargsmyres	edns_sizedomainsr   rl   rm   rn   ro   rp   rG   rx   etnssetnsnsaddroptargdomainreversed_domainsnretanswersrW   sr]   errs                                  ra   runzLookupModule.runO  s     uoppYv> %%%5	qCIILL)D(v&8__\2
ooe$v&	Ann..tw/GHG  $/?@ 2	"A||C !"C( FBF((,#**2.F ax wwsAHC '>IIKEF]s8DG^M"%..":":3"? ,,+23<E(O+$+CLML(!(JE\!#,Cax&$%GGCLMFENN6* q!e2	"l EJ{a +E;;=E!!! SS44V<A$++AIIK8S 'Gw<!J #	NF"N++feW#+N$ 1EA{{}-aG

11!0!7B*1*@*@*H*H*JBwK),)>)>u||)LBvJ(/(9(9BuI*-..*@*@*OBwKJJrN1#	NJ 
k  	A!;A3?@@	A % FF%(\\%7%7%;A%>%F%FF'..v6( F"./D"EEF 7F !  % M*-Gs+KLLM" ! &NN1%&$ }}00   S&)MaS'QRRS8  ) 1,&2_SE3J&K KJJs3x001
 <<(( + &'>??!JJz*LL))3<<+?+?A[A[\ # &'>??!JJrN==-- N"%Fse#LMMNs   .P &P*RR(4%S(>S(AU'B*T&U'	P'P""P'*	R4=Q21R2	R	;R			R	RR	R%$R%(	S1R??SS%$S%(T#T#TT#&	U$/*UU'U$$U''Y#V--AY #X)) Y	YY)N)__name__
__module____qualname__r    rc   ra   re   re   M  s    Yrc   re   )3
__future__r   r   r   r{   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.errorsr   ansible.plugins.lookupr   )ansible.module_utils.parsing.convert_boolr   ansible.utils.displayr	   r   dns.exceptionrS   dns.namedns.resolverdns.reversenamedns.rdataclassdns.rdatatyper
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   ImportErrordisplayrb   re   r   rc   ra   <module>r      s    C BiV1fM
^ ( - = ) 
d d d d d dH
 )9B[: [Q  Hs   AB
 
BB