
    Vh!                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZmZ d dlmZmZmZ d dlmZmZmZ d d	lmZmZmZ dZ	 d dlZd
Zd Zd Z d Z! G d de      Z"e#dk(  r e        yy# e$ r  e
j<                         ZdZY <w xY w)    )absolute_importdivisionprint_functiona  
module: ldap_search
version_added: '0.2.0'
short_description: Search for entries in a LDAP server
description:
  - Return the results of an LDAP search.
author:
  - Sebastian Pfahl (@eryx12o45)
requirements:
  - python-ldap
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  dn:
    required: true
    type: str
    description:
      - The LDAP DN to search in.
  scope:
    choices: [base, onelevel, subordinate, children]
    default: base
    type: str
    description:
      - The LDAP scope to use.
      - V(subordinate) requires the LDAPv3 subordinate feature extension.
      - V(children) is equivalent to a "subtree" scope.
  filter:
    default: '(objectClass=*)'
    type: str
    description:
      - Used for filtering the LDAP search result.
  attrs:
    type: list
    elements: str
    description:
      - A list of attributes for limiting the result. Use an actual list or a comma-separated string.
  schema:
    default: false
    type: bool
    description:
      - Set to V(true) to return the full attribute schema of entries, not their attribute values. Overrides O(attrs) when
        provided.
  page_size:
    default: 0
    type: int
    description:
      - The page size when performing a simple paged result search (RFC 2696). This setting can be tuned to reduce issues
        with timeouts and server limits.
      - Setting the page size to V(0) (default) disables paged searching.
    version_added: 7.1.0
  base64_attributes:
    description:
      - If provided, all attribute values returned that are listed in this option will be Base64 encoded.
      - If the special value V(*) appears in this list, all attributes will be Base64 encoded.
      - All other attribute values will be converted to UTF-8 strings. If they contain binary data, please note that invalid
        UTF-8 bytes will be omitted.
    type: list
    elements: str
    version_added: 7.0.0
extends_documentation_fragment:
  - community.general.ldap.documentation
  - community.general.attributes
a`  
- name: Return all entries within the 'groups' organizational unit.
  community.general.ldap_search:
    dn: "ou=groups,dc=example,dc=com"
  register: ldap_groups

- name: Return GIDs for all groups
  community.general.ldap_search:
    dn: "ou=groups,dc=example,dc=com"
    scope: "onelevel"
    attrs:
      - "gidNumber"
  register: ldap_group_gids
a]  
results:
  description:
    - For every entry found, one dictionary will be returned.
    - Every dictionary contains a key C(dn) with the entry's DN as a value.
    - Every attribute of the entry found is added to the dictionary. If the key has precisely one value, that value is taken
      directly, otherwise the key's value is a list.
    - Note that all values (for single-element lists) and list elements (for multi-valued lists) will be UTF-8 strings. Some
      might contain Base64-encoded binary data; which ones is determined by the O(base64_attributes) option.
  type: list
  elements: dict
N)AnsibleModulemissing_required_lib)to_bytes	to_nativeto_text)binary_typestring_types	text_type)LdapGeneric	gen_specsldap_required_togetherTFc                     t        t        t        dd      t        ddg d      t        dd      t        d	d
      t        dd      t        dd      t        d	d
            dt                     } t        s | j                  t        d      t               	 t        |       j                          y # t        $ r&}| j                  dt        |             Y d }~y d }~ww xY w)NstrT)typerequiredbaser   onelevelsubordinatechildren)r   defaultchoicesz(objectClass=*))r   r   list)r   elementsboolFintr   )dnscopefilterattrsschema	page_sizebase64_attributes)argument_specsupports_check_moderequired_togetherzpython-ldap)msg	exceptionzAttribute action failed.)r*   details)r   r   dictr   HAS_LDAP	fail_jsonr   LDAP_IMP_ERR
LdapSearchmain	Exceptionr	   )moduler+   s     q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ldap_search.pyr2   r2   }   s    .E6;jkU,=>FU3VU3q1"?
 !02F 1-@#/ 	 	1W6! W79AUVVWs   B8 8	C'C""C'c                     t        | t        t        f      rMt        | t              rt	        | d      } |r t        t        j                  |             } | S t        | dd      } | S )Nzutf-8)encodingreplace)errors)
isinstancer   r   r   r   r
   base64	b64encode)valconvert_to_base64s     r5   _normalize_stringr?      s[    #k23c9%31C&**3/0C J #wy9CJ    c                     d| i}t        |j                               D ]M  \  }}d|v xs ||v }t        |      dk(  rt        |d   |      ||<   1|D cg c]  }t        ||       c}||<   O |S c c}w )Nr    *   r   )r   itemslenr?   )r    r#   r&   	extractedattrr=   r>   vs           r5   _extract_entryrI      s    r
I%++-( U	c#44Q@Q8Qs8q=/A8IJIdOPST104EFTIdOU  Us   A0c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)r1   c                 l   t        j                  | |       | j                  j                  d   | _        g | _        | j                  j                  d   | _        | j                          | j                          | j                          t        | j                  j                  d   xs g       | _        y )Nr"   r%   r&   )r   __init__r4   params	filterstrattrlistr%   _load_scope_load_attrs_load_schemaset_base64_attributes)selfr4   s     r5   rL   zLdapSearch.__init__   s    T6*++H5++K8"%dkk&8&89L&M&SQS"Tr@   c                 v    | j                   j                  d   | _        | j                  rd| _        y d| _        y )Nr$   rC   r   )r4   rM   r$   	attrsonlyrU   s    r5   rR   zLdapSearch._load_schema   s-    kk((2;;DNDNr@   c                     t        t        j                  t        j                  t        j                  t        j
                        }|| j                  j                  d      | _        y )Nr   r!   )	r-   ldap
SCOPE_BASESCOPE_ONELEVELSCOPE_SUBORDINATESCOPE_SUBTREEr4   rM   r!   )rU   specs     r5   rP   zLdapSearch._load_scope   sK    ((..''	
 $++,,W56
r@   c                 H    | j                   j                  d   xs d | _        y )Nr#   )r4   rM   rO   rX   s    r5   rQ   zLdapSearch._load_attrs   s    **73;tr@   c                 ^    | j                         }| j                  j                  d|       y )NF)changedresults)perform_searchr4   	exit_json)rU   rc   s     r5   r2   zLdapSearch.main   s'    %%'eW=r@   c           
      V   g }g }| j                   dkD  rE|j                  t        j                  j                  j                  d| j                   d             	 	 | j                  j                  | j                  | j                  | j                  | j                  | j                  |      }| j                  j                  |      \  }}}}|D ]  }t        |d   t              s| j                   r:|j                  t        |d   t#        |d   j%                                            ]|j                  t'        |d   |d   | j(                                |D 	cg c]J  }	|	j*                  t        j                  j                  j
                  j*                  k(  s?|	j,                  L }
}	| j                   dkD  r|
r|
d   r|
d   |d   _        n|S }c c}	w # t        j.                  $ r8 | j0                  j3                  dj5                  | j                        	       Y y w xY w)
Nr   T )sizecookie)rN   rO   rW   serverctrlsrC   )r    r#   zBase not found: {0})r*   )r%   appendrZ   controlslibldapSimplePagedResultsControl
connection
search_extr    r!   rN   rO   rW   result3r:   r-   r$   r   keysrI   rT   controlTyperi   NO_SUCH_OBJECTr4   r/   format)rU   ldap_entriesrl   responsertyperc   rmsgidrj   resultccookiess              r5   rd   zLdapSearch.perform_search   s   >>AOODMM11KKDW[WeWenpKqr	M??55GGJJ"nn!]]"nn ( 6  7;oo6M6Mh6W3w% oF!&)T2;;(//q	fUVinnN^I_0`a(//vay&QR)UYUlUl0mno .9  JAMMT]]MbMbM|M|  NI  NI  =I188  J  J>>A%'gaj)0HQK&'')   J
 "" 	MKK!!&;&B&B477&K!L	Ms4   BG 'A8G A G G.'G G AH('H(N)	__name__
__module____qualname__rL   rR   rP   rQ   r2   rd    r@   r5   r1   r1      s"    	U7<>Mr@   r1   __main__)$
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRESULTSr;   	tracebackansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r	   r
   ansible.module_utils.sixr   r   r   ?ansible_collections.community.general.plugins.module_utils.ldapr   r   r   r0   rZ   r.   ImportError
format_excr2   r?   rI   r1   r}   r   r@   r5   <module>r      s    A @AF   J T T I I z zHW2?M ?MD zF u  '9'')LHs   A, ,BB