a
    Wa9                     @   s   d dl mZ d dlZd dlmZ d dlmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ G dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd de
ZdS )    N)system_session)CommandCommandErrorOptionSuperCommand)SamDB)dsdb)
attrgetterc                   @   sN   e Zd ZdZdZedddedddgZd	d
gZe	j
e	je	jdZdddZdS )
cmd_renamea	  Rename an organizational unit.

    The name of the organizational units can be specified as a full DN
    or without the domainDN component.

    Examples:
    samba-tool ou rename 'OU=OrgUnit,DC=samdom,DC=example,DC=com' \
        'OU=NewNameOfOrgUnit,DC=samdom,DC=example,DC=com'
    samba-tool ou rename 'OU=OrgUnit' 'OU=NewNameOfOrgUnit'

    The examples show how an administrator would rename an ou 'OrgUnit'
    to 'NewNameOfOrgUnit'. The new DN would be
    'OU=NewNameOfOrgUnit,DC=samdom,DC=example,DC=com'
    z'%prog <old_ou_dn> <new_ou_dn> [options]-H--URL%LDB URL for database or target serverURLHhelptypemetavardest	old_ou_dn	new_ou_dn	sambaoptscredoptsversionoptsNc              
   C   sT  |  }|j|dd}t|t ||d}	t|	|	 }
z|	|}W n4 tyz } zt	d||f W Y d }~n
d }~0 0 z|	|}W n4 ty } zt	d||f W Y d }~n
d }~0 0 zF|	j
|dtjg d}t|dkr| jd	|  W d S |	|| W n4 ty: } zt	d
| |W Y d }~n
d }~0 0 | jd||f  d S )NTZfallback_machineZurlZsession_infoZcredentialslpInvalid old_ou_dn "%s": %szInvalid new_ou_dn "%s": %s (objectclass=organizationalUnit)base
expressionscopeattrsr   Unable to find ou "%s"
zFailed to rename ou "%s"zRenamed ou "%s" to "%s"
)get_loadparmget_credentialsr   r   ldbDn	domain_dnnormalize_dn_in_domain	Exceptionr   search
SCOPE_BASElenoutfwriterename)selfr   r   r   r   r   r   r   credssamdbr*   full_old_ou_dnefull_new_ou_dnres r:   1/usr/lib/python3/dist-packages/samba/netcmd/ou.pyrun@   s@    $
zcmd_rename.run)NNNN__name__
__module____qualname____doc__synopsisr   strtakes_optionsZ
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsr<   r:   r:   r:   r;   r
   "   s     r
   c                   @   sN   e Zd ZdZdZedddedddgZd	d
gZe	j
e	je	jdZdddZdS )cmd_moveai  Move an organizational unit.

    The name of the organizational units can be specified as a full DN
    or without the domainDN component.

    Examples:
    samba-tool ou move 'OU=OrgUnit,DC=samdom,DC=example,DC=com' \
        'OU=NewParentOfOrgUnit,DC=samdom,DC=example,DC=com'
    samba-tool ou rename 'OU=OrgUnit' 'OU=NewParentOfOrgUnit'

    The examples show how an administrator would move an ou 'OrgUnit'
    into the ou 'NewParentOfOrgUnit'. The ou 'OrgUnit' would become
    a child of the 'NewParentOfOrgUnit' ou. The new DN would be
    'OU=OrgUnit,OU=NewParentOfOrgUnit,DC=samdom,DC=example,DC=com'
    z+%prog <old_ou_dn> <new_parent_dn> [options]r   r   r   r   r   r   r   new_parent_dnr   Nc              
   C   s  |  }|j|dd}t|t ||d}	t|	|	 }
z|	|}W n4 tyz } zt	d||f W Y d }~n
d }~0 0 z|	|}W n4 ty } zt	d||f W Y d }~n
d }~0 0 t|	t
|}|t|d  || zH|	j|dtjg d}t|d	kr&| jd
|  W d S |	|| W n4 tyh } zt	d| |W Y d }~n
d }~0 0 | jd||f  d S )NTr   r   r   zInvalid new_parent_dn "%s": %s   r   r    r   r%   zFailed to move ou "%s"zMoved ou "%s" into "%s"
)r&   r'   r   r   r(   r)   r*   r+   r,   r   rC   remove_base_componentsr/   Zadd_baser-   r.   r0   r1   r2   )r3   r   rK   r   r   r   r   r   r4   r5   r*   r6   r7   Zfull_new_parent_dnr8   r9   r:   r:   r;   r<      sF    
$zcmd_move.run)NNNNr=   r:   r:   r:   r;   rJ   b   s     rJ   c                   @   sZ   e Zd ZdZdZedddeddded	d
eddgZdgZe	j
e	je	jdZdddZdS )
cmd_createa  Create an organizational unit.

    The name of the new ou can be specified as a full DN or without the
    domainDN component.

    Examples:
    samba-tool ou create 'OU=OrgUnit'
    samba-tool ou create 'OU=SubOU,OU=OrgUnit,DC=samdom,DC=example,DC=com'

    The examples show how an administrator would create a new ou 'OrgUnit'
    and a new ou 'SubOU' as a child of the ou 'OrgUnit'.
    %prog <ou_dn> [options]r   r   r   r   r   r   z--descriptionzOU's descriptiondescription)r   r   r   ou_dnr   Nc              
   C   s   |  }|j|dd}t|t ||d}	z|	|}
W n4 tyj } ztd||f W Y d }~n
d }~0 0 z|	j|
|d W n2 ty } ztd|
 |W Y d }~n
d }~0 0 | j	d|
  d S )NTr   r   Invalid ou_dn "%s": %s)rP   zFailed to create ou "%s"zCreated ou "%s"
)
r&   r'   r   r   r+   r,   r   Z	create_our0   r1   )r3   rQ   r   r   r   r   rP   r   r4   r5   
full_ou_dnr7   r:   r:   r;   r<      s    &$zcmd_create.run)NNNNNr=   r:   r:   r:   r;   rN      s"     rN   c                
   @   sn   e Zd ZdZdZedddeddded	d
ddddedddddddgZdgZe	j
e	je	jdZdddZdS )cmd_listobjectsa  List all objects in an organizational unit.

    The name of the organizational unit can be specified as a full DN
    or without the domainDN component.

    Examples:
    samba-tool ou listobjects 'OU=OrgUnit,DC=samdom,DC=example,DC=com'
    samba-tool ou listobjects 'OU=OrgUnit'

    The examples show how an administrator would list all child objects
    of the ou 'OrgUnit'.
    rO   r   r   r   r   r   r   	--full-dnfull_dnF
store_true"Display DNs including the base DN.r   defaultactionr   z-rz--recursive	recursivezList objects recursively.rQ   r   Nc              
   C   sN  |  }|j|dd}	t|t |	|d}
t|
|
 }z|
|}W n4 tyz } zt	d||f W Y d }~n
d }~0 0 d}tj
}|rd}tj}z|
j|d|g d}t||kr| jd	|  W d S t|td
dD ]6}|j|krq|s|jt| | jd|j  qW n4 tyH } zt	d| |W Y d }~n
d }~0 0 d S )NTr   r   rR   r   rL   z(objectclass=*)r    zou "%s" is empty
dnkey%s
z"Failed to list contents of ou "%s")r&   r'   r   r   r(   r)   r*   r+   r,   r   ZSCOPE_ONELEVELSCOPE_SUBTREEr-   r/   r0   r1   sortedr	   r]   rM   )r3   rQ   r   r   r   r   rV   r\   r   r4   r5   r*   rS   r7   Z	minchildsr#   Zchildschildr:   r:   r;   r<      sF    &
zcmd_listobjects.run)NNNNFFr=   r:   r:   r:   r;   rT      s(   

	  rT   c                	   @   sd   e Zd ZdZdZedddeddded	d
dededdddddgZej	ej
ejdZdddZdS )cmd_listzList all organizational units.

    Example:
    samba-tool ou listobjects

    The example shows how an administrator would list all organizational
    units.
    z%prog [options]r   r   r   r   r   r   z-bz	--base-dnzSpecify base DN to use.)r   r   rU   rV   FrW   rX   rY   r   Nc                 C   s   |  }|j|dd}t|t ||d}	t|	|	 }
|rF|	|}
|	j|
tj	dg d}t
|dkrjd S t|tddD ]>}|st|	|	 }|jt
| | jd	t|j  qzd S )
NTr   r   z (objectClass=organizationalUnit))r#   r"   r$   r   r]   r^   r`   )r&   r'   r   r   r(   r)   r*   r+   r-   ra   r/   rb   r	   r]   rM   r0   r1   rC   )r3   r   r   r   r   Zbase_dnrV   r   r4   r5   Z	search_dnr9   msgr*   r:   r:   r;   r<   :  s(    
zcmd_list.run)NNNNNF)r>   r?   r@   rA   rB   r   rC   rD   rE   rF   rG   rH   rI   r<   r:   r:   r:   r;   rd     s0   	
      rd   c                   @   s\   e Zd ZdZdZedddeddded	d
ddddgZdgZe	j
e	je	jdZdddZdS )
cmd_deleteaS  Delete an organizational unit.

    The name of the organizational unit can be specified as a full DN
    or without the domainDN component.

    Examples:
    samba-tool ou delete 'OU=OrgUnit,DC=samdom,DC=example,DC=com'
    samba-tool ou delete 'OU=OrgUnit'

    The examples show how an administrator would delete the ou 'OrgUnit'.
    rO   r   r   r   r   r   r   z--force-subtree-deleteforce_subtree_deleteFrW   z7Delete organizational unit and all children reclusivelyrY   rQ   r   Nc              
   C   s  |  }|j|dd}t|t ||d}	t|	|	 }
z|	|}W n4 tyz } zt	d||f W Y d }~n
d }~0 0 g }|rdg}zF|	j
|dtjg d}t|dkr| jd	|  W d S |	|| W n4 ty } zt	d
| |W Y d }~n
d }~0 0 | jd|  d S )NTr   r   rR   ztree_delete:1r   r    r   r%   zFailed to delete ou "%s"zDeleted ou "%s"
)r&   r'   r   r   r(   r)   r*   r+   r,   r   r-   r.   r/   r0   r1   delete)r3   rQ   r   r   r   r   rg   r   r4   r5   r*   rS   r7   Zcontrolsr9   r:   r:   r;   r<   v  s2    &$zcmd_delete.run)NNNNFr=   r:   r:   r:   r;   rf   X  s$     rf   c                   @   sP   e Zd ZdZi Ze ed< e ed< e ed< e ed< e	 ed< e
 ed< dS )	cmd_ouz%Organizational Units (OU) management.Zcreaterh   Zmover2   listZlistobjectsN)r>   r?   r@   rA   ZsubcommandsrN   rf   rJ   r
   rd   rT   r:   r:   r:   r;   ri     s   




ri   )Zsamba.getoptZgetoptrE   r(   Z
samba.authr   Zsamba.netcmdr   r   r   r   Zsamba.samdbr   Zsambar   operatorr	   r
   rJ   rN   rT   rd   rf   ri   r:   r:   r:   r;   <module>   s   @D2F:=