
    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
mZmZ d dlmZmZ d dlmZmZmZ dZ	 d dlZd dlZd	Z G d de      Zd Zedk(  r e        yy# e$ r  e	j4                         Zd
ZY 6w xY w)    )absolute_importdivisionprint_functiona  
module: ldap_entry
short_description: Add or remove LDAP entries
description:
  - Add or remove LDAP entries. This module only asserts the existence or non-existence of an LDAP entry, not its attributes.
    To assert the attribute values of an entry, see M(community.general.ldap_attrs).
author:
  - Jiri Tyr (@jtyr)
requirements:
  - python-ldap
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  attributes:
    description:
      - If O(state=present), attributes necessary to create an entry. Existing entries are never modified. To assert specific
        attribute values on an existing entry, use M(community.general.ldap_attrs) module instead.
      - Each attribute value can be a string for single-valued attributes or a list of strings for multi-valued attributes.
      - If you specify values for this option in YAML, please note that you can improve readability for long string values
        by using YAML block modifiers as seen in the examples for this module.
      - Note that when using values that YAML/ansible-core interprets as other types, like V(yes), V(no) (booleans), or V(2.10)
        (float), make sure to quote them if these are meant to be strings. Otherwise the wrong values may be sent to LDAP.
    type: dict
    default: {}
  objectClass:
    description:
      - If O(state=present), value or list of values to use when creating the entry. It can either be a string or an actual
        list of strings.
    type: list
    elements: str
  state:
    description:
      - The target state of the entry.
    choices: [present, absent]
    default: present
    type: str
  recursive:
    description:
      - If O(state=delete), a flag indicating whether a single entry or the whole branch must be deleted.
    type: bool
    default: false
    version_added: 4.6.0
extends_documentation_fragment:
  - community.general.ldap.documentation
  - community.general.attributes
a\  
- name: Make sure we have a parent entry for users
  community.general.ldap_entry:
    dn: ou=users,dc=example,dc=com
    objectClass: organizationalUnit

- name: Make sure we have an admin user
  community.general.ldap_entry:
    dn: cn=admin,dc=example,dc=com
    objectClass:
      - simpleSecurityObject
      - organizationalRole
    attributes:
      description: An LDAP administrator
      userPassword: "{SSHA}tabyipcHzhwESzRaGA7oQ/SDoBZQOGND"

- name: Set possible values for attributes elements
  community.general.ldap_entry:
    dn: cn=admin,dc=example,dc=com
    objectClass:
      - simpleSecurityObject
      - organizationalRole
    attributes:
      description: An LDAP Administrator
      roleOccupant:
        - cn=Chocs Puddington,ou=Information Technology,dc=example,dc=com
        - cn=Alice Stronginthebrain,ou=Information Technology,dc=example,dc=com
      olcAccess:
        - >-
          {0}to attrs=userPassword,shadowLastChange
          by self write
          by anonymous auth
          by dn="cn=admin,dc=example,dc=com" write
          by * none'
        - >-
          {1}to dn.base="dc=example,dc=com"
          by dn="cn=admin,dc=example,dc=com" write
          by * read

- name: Get rid of an old entry
  community.general.ldap_entry:
    dn: ou=stuff,dc=example,dc=com
    state: absent
    server_uri: ldap://localhost/
    bind_dn: cn=admin,dc=example,dc=com
    bind_pw: password

#
# The same as in the previous example but with the authentication details
# stored in the ldap_auth variable:
#
# ldap_auth:
#   server_uri: ldap://localhost/
#   bind_dn: cn=admin,dc=example,dc=com
#   bind_pw: password
#
# In the example below, 'args' is a task keyword, passed at the same level as the module
- name: Get rid of an old entry
  community.general.ldap_entry:
    dn: ou=stuff,dc=example,dc=com
    state: absent
  args: "{{ ldap_auth }}"
z
# Default return values
N)AnsibleModulemissing_required_lib)	to_nativeto_bytes)LdapGeneric	gen_specsldap_required_togetherTFc                   *    e Zd Zd Zd Zd Zd Zd Zy)	LdapEntryc                 X   t        j                  | |       | j                  j                  d   | _        | j                  j                  d   | _        | j                  j                  d   | j                  j                  d   d<   | j                  dk(  r| j                         | _        y y )Nstate	recursiveobjectClass
attributespresent)r
   __init__moduleparamsr   r   _load_attrsattrs)selfr   s     p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ldap_entry.pyr   zLdapEntry.__init__   s    T6* [[''0
++K8 KK}- 	<(7 ::"))+DJ #    c                     i }| j                   j                  d   j                         D ]A  \  }}t        |t              rt	        t        t        |            ||<   3t        |      g||<   C |S )z" Turn attribute's value to array. r   )r   r   items
isinstancelistmapr	   )r   r   namevalues       r   r   zLdapEntry._load_attrs   sh    ;;--l;AAC 	0KD%%&"3x#78d'/d		0 r   c                       fd} j                         s-t        j                  j                   j                        |}|S d}|S )zA If self.dn does not exist, returns a callable that will add it. c                  R    j                   j                  j                          y N)
connectionadd_sdn)modlistr   s   r   _addzLdapEntry.add.<locals>._add   s    OO!!$''73r   N)_is_entry_presentldapr*   
addModlistr   )r   r+   actionr*   s   `  @r   addzLdapEntry.add   sF    	4 %%'ll--djj9GF  Fr   c                 h      fd} fd} j                         r j                  r|}|S |}|S d}|S )z If self.dn exists, returns a callable that will delete either
        the item itself if the recursive option is not set or the whole branch
        if it is. c                  P     j                   j                   j                         y r&   )r'   delete_sr)   )r   s   r   _deletez!LdapEntry.delete.<locals>._delete   s    OO$$TWW-r   c                     	 t         j                  j                  d      } j                  j	                  j
                  | g       y# t         j                  $ rn j                  j                  j
                  t         j                  d      }|j                          |D ]   }j                  j                  |d          " Y yw xY w)zi Attempt recursive deletion using the subtree-delete control.
            If that fails, do it manually. z1.2.840.113556.1.4.805)serverctrls)r)   )attrlistr   N)r-   controlsValueLessRequestControlr'   delete_ext_sr)   NOT_ALLOWED_ON_NONLEAFsearch_sSCOPE_SUBTREEreverser3   )subtree_deletesearchentryr   s      r   _delete_recursivez+LdapEntry.delete.<locals>._delete_recursive   s    7!%!F!FG_!`,,TWW>BR,S.. 711$''4;M;MX_1` # 7EOO,,U1X677s   AA A>CCN)r,   r   )r   r4   rB   r/   s   `   r   deletezLdapEntry.delete   sH    	.
	7 !!#~~* 	 !  Fr   c                     	 | j                   j                  | j                  t        j                         d}|S # t        j
                  $ r d}Y |S w xY w)NTF)r'   r<   r)   r-   
SCOPE_BASENO_SUCH_OBJECT)r   
is_presents     r   r,   zLdapEntry._is_entry_present   sT    	OO$$TWWdoo> J "" 	J 	s   4: AAN)__name__
__module____qualname__r   r   r0   rC   r,    r   r   r   r      s    ,
:r   r   c                  j   t        t        t        i d      t        dd      t        dddg      t        d	d
            dddgfgdt                     } t        s | j                  t        d      t               | j                  d   }t        |       }|dk(  r |j                         }n|dk(  r |j                         }| j                  s	  |        | j#                  |d u       y # t        $ r9}| j                  dt        |      t        j                                 Y d }~Rd }~ww xY w)Ndict)defaulttyper    str)rO   elementsr   absent)rN   choicesFbool)r   r   r   r   r   r   T)argument_specrequired_ifsupports_check_moderequired_togetherzpython-ldap)msg	exceptionzEntry action failed.)rY   detailsrZ   )changed)r   r   rM   r   HAS_LDAP	fail_jsonr   LDAP_IMP_ERRr   r   r0   rC   
check_mode	Exceptionr   	traceback
format_exc	exit_json)r   r   r-   r/   es        r   mainrf      s4   BV4&59y9h2GH5v6	
 y=/:; 02
F 1-@#/ 	 	1 MM'"E VD 		(	 &"3"3	qH fD02  	q!71YbYmYmYopp	qs   C0 0	D29/D--D2__main__)
__future__r   r   r   rO   __metaclass__DOCUMENTATIONEXAMPLESRETURNrb   ansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r	   ?ansible_collections.community.general.plugins.module_utils.ldapr
   r   r   r_   ldap.modlistr-   ldap.controlsr]   ImportErrorrc   r   rf   rH   rK   r   r   <module>rs      s    A @0f>B
  J K z zHN Nb#3L zF y  '9'')LHs   
A A54A5