
    b9                     &   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e          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de
          ZdS )    N)system_session)CommandCommandErrorOptionSuperCommand)SamDB)dsdb)
attrgetterc                   x    e Zd ZdZdZ edddedd          gZd	d
gZe	j
        e	j        e	j        dZ	 	 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                 F   |                                 }|                    |d          }t          |t                      ||          }	t	          j        |	|	                                          }
	 |	                    |          }n'# t          $ r}t          d|d|          d }~ww xY w	 |	                    |          }n'# t          $ r}t          d|d|          d }~ww xY w	 |	
                    |dt          j        g           }t          |          d	k    r| j                            d
|z             d S |	                    ||           n%# t          $ r}t          d|z  |          d }~ww xY w| j                            d|d|d           d S )NTfallback_machineurlsession_infocredentialslpInvalid old_ou_dn "": zInvalid new_ou_dn " (objectclass=organizationalUnit)base
expressionscopeattrsr   Unable to find ou "%s"
zFailed to rename ou "%s"zRenamed ou "z" to ""
)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samdbr3   full_old_ou_dnefull_new_ou_dnress                  1/usr/lib/python3/dist-packages/samba/netcmd/ou.pyrunzcmd_rename.run@   s	   ##%%((d(CC!.*:*:"'B0 0 0F5%//"3"344		/"99)DDNN 	/ 	/ 	/, )		11 . / / /	/	/"99)DDNN 	/ 	/ 	/, )		11 . / / /	/
	O,,N*L%(^2  ? ?C 3xx1}}	 :Y FGGGLL8888 	O 	O 	O9NJANNN	O	~~~7E~~G 	H 	H 	H 	H 	HsO   3B	 	
B-B((B-1C 
C+C&&C+/AE E 
E=%E88E=NNNN__name__
__module____qualname____doc__synopsisr   strtakes_options
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsrD        rC   r   r   "   s          9H 	tW#JS	2 	2 	2M
 {+J).-  BF $H H H H H HrU   r   c                   x    e Zd ZdZdZ edddedd          gZd	d
gZe	j
        e	j        e	j        dZ	 	 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                    |                                 }|                    |d          }t          |t                      ||          }	t	          j        |	|	                                          }
	 |	                    |          }n'# t          $ r}t          d|d|          d }~ww xY w	 |	                    |          }n'# t          $ r}t          d|d|          d }~ww xY wt	          j        |	t          |                    }|                    t          |          dz
             |                    |           	 |	                    |dt          j        g 	          }t          |          d
k    r| j                            d|z             d S |	                    ||           n%# t          $ r}t          d|z  |          d }~ww xY w| j                            d|d|d           d S )NTr   r    r%   r&   zInvalid new_parent_dn "   r'   r(   r   r-   zFailed to move ou "%s"z
Moved ou "z" into "r.   )r/   r0   r   r   r1   r2   r3   r4   r5   r   rL   remove_base_componentsr8   add_baser6   r7   r9   r:   r;   )r<   r   rX   r   r   r   r   r$   r=   r>   r3   r?   r@   full_new_parent_dnrA   rB   s                   rC   rD   zcmd_move.run   s[   ##%%((d(CC!.*:*:"'B0 0 0 F5%//"3"344		/"99)DDNN 	/ 	/ 	/, )		11 . / / /	/	3!&!=!=m!L!L 	3 	3 	3, -qq 2 3 3 3	3 s>':':;;--c..A.AA.EFFF 2333		M,,N*L%(^2  ? ?C 3xx1}}	 :^ KLLLLL8888 	M 	M 	M7.H!LLL	M	');););= 	> 	> 	> 	> 	>sO   3B	 	
B-B((B-1C 
C+C&&C+AF7  F7 7
GGGrE   rF   rT   rU   rC   rW   rW   b   s           =H 	tW#JS	2 	2 	2M
 /J).-  FJ $"> "> "> "> "> ">rU   rW   c                       e Zd ZdZdZ edddedd           ed	d
ed          gZdgZe	j
        e	j        e	j        dZ	 	 ddZdS )cmd_adda  Add a new organizational unit.

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

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

    The examples show how an administrator would add 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                    |                                 }|                    |d          }t          |t                      ||          }		 |	                    |          }
n'# t
          $ r}t          d|d|          d }~ww xY w	 |	                    |
|           n%# t
          $ r}t          d|
z  |          d }~ww xY w| j        	                    d|
z             d S )	NTr   r    Invalid ou_dn "r&   )ra   zFailed to add ou "%s"zAdded ou "%s"
)
r/   r0   r   r   r4   r5   r   	create_our9   r:   )r<   rb   r   r   r   r   ra   r$   r=   r>   
full_ou_dnr@   s               rC   rD   zcmd_add.run   s!   ##%%((d(CC!.*:*:"'B0 0 0	F55e<<JJ 	F 	F 	F,555!!DEEE	F	HOOJKO@@@@ 	H 	H 	H6CQGGG	H 		)J677777s0   A" "
B,BB
B" "
C,B??C)NNNNNrF   rT   rU   rC   r_   r_      s          )H 	tW#JS	2 	2 	2%7m	- 	- 	-M J).-  EI $8 8 8 8 8 8rU   r_   c            
           e Zd ZdZdZ edddedd           ed	d
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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'.
    r`   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.rb   r   Nc                 r   |                                 }|                    |d          }	t          |t                      |	|          }
t	          j        |
|
                                          }	 |
                    |          }n'# t          $ r}t          d|d|          d }~ww xY wd}t          j
        }|rd}t          j        }	 |
                    |d|g 	          }t          |          |k    r| j                            d
|z             d S t!          |t#          d                    D ]Y}|j        |k    r|s'|j                            t          |                     | j                            d|j        z             Zd S # t          $ r}t          d|z  |          d }~ww xY w)NTr   r    rd   r&   r   rZ   z(objectclass=*)r(   zou "%s" is empty
dnkey%s
z"Failed to list contents of ou "%s")r/   r0   r   r   r1   r2   r3   r4   r5   r   SCOPE_ONELEVELSCOPE_SUBTREEr6   r8   r9   r:   sortedr
   rr   r[   )r<   rb   r   r   r   r   rj   rp   r$   r=   r>   r3   rf   r@   	minchildsr+   childschilds                     rC   rD   zcmd_listobjects.run   s   ##%%((d(CC!.*:*:"'B0 0 0F5%//"3"344		F55e<<JJ 	F 	F 	F,555!!DEEE	F 	" 	&I%E	.\\z->(-R " 9 9F 6{{i''	 4u <===Jt,<,<=== 3 38z)) DH33C	NNCCC	 122223 3  	. 	. 	.C) *+,. . .	.s8   3B	 	
B-B((B-A	F A8F 
F6F11F6)NNNNFFrF   rT   rU   rC   rh   rh      s          )H 	tW#JS	2 	2 	2{E,8	: 	: 	:t]e")D	F 	F 	FM J).-  EI-2$. $. $. $. $. $.rU   rh   c            	           e Zd ZdZdZ edddedd           ed	d
de           eddddd          gZej	        ej
        ej        dZ	 	 	 	 	 	 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   ri   rj   Frk   rl   rm   r   Nc                    |                                 }|                    |d          }t          |t                      ||          }	t	          j        |	|	                                          }
|r|	                    |          }
|	                    |
t          j	        dg           }t          |          dk    rd S t          |t          d                    D ]}|sNt	          j        |	|	                                          }|j                            t          |                     | j                            d	t#          |j                  z             d S )
NTr   r    z (objectClass=organizationalUnit))r+   r*   r,   r   rr   rs   ru   )r/   r0   r   r   r1   r2   r3   r4   r6   rw   r8   rx   r
   rr   r[   r9   r:   rL   )r<   r   r   r   r   base_dnrj   r$   r=   r>   	search_dnrB   msgr3   s                 rC   rD   zcmd_list.run:  sU    ##%%((d(CC!.*:*:"'B0 0 0 F5%//"3"344	 	>44W==Ill9!$!2&H!#  % % HHMMF#:d#3#3444 	2 	2C >F5%//*;*;<<	--c)nn===IOOFS[[01111		2 	2rU   )NNNNNF)rG   rH   rI   rJ   rK   r   rL   rM   rO   rP   rQ   rR   rS   rD   rT   rU   rC   r}   r}     s          !H 	tW#JS	2 	2 	2t[-	 	 	 	{E,8	: 	: 	:M ).-  2 2 2 2 2 2rU   r}   c                       e Zd ZdZdZ edddedd           ed	d
ddd          gZdgZe	j
        e	j        e	j        dZ	 	 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'.
    r`   r   r   r   r   r   r   z--force-subtree-deleteforce_subtree_deleteFrk   z7Delete organizational unit and all children reclusivelyrm   rb   r   Nc                    |                                 }|                    |d          }t          |t                      ||          }	t	          j        |	|	                                          }
	 |	                    |          }n'# t          $ r}t          d|d|          d }~ww xY wg }|rdg}	 |	
                    |dt          j        g           }t          |          d	k    r| j                            d
|z             d S |	                    ||           n%# t          $ r}t          d|z  |          d }~ww xY w| j                            d|z             d S )NTr   r    rd   r&   ztree_delete:1r'   r(   r   r-   zFailed to delete ou "%s"zDeleted ou "%s"
)r/   r0   r   r   r1   r2   r3   r4   r5   r   r6   r7   r8   r9   r:   delete)r<   rb   r   r   r   r   r   r$   r=   r>   r3   rf   r@   controlsrB   s                  rC   rD   zcmd_delete.runv  s   ##%%((d(CC!.*:*:"'B0 0 0F5%//"3"344		F55e<<JJ 	F 	F 	F,555!!DEEE	F  	)'(H		K,,J*L%(^2  ? ?C 3xx1}}	 :U BCCCLLX.... 	K 	K 	K9JFJJJ	K 		+j899999s7   3B	 	
B-B((B-8AD$ D$ $
E.EE)NNNNFrF   rT   rU   rC   r   r   X  s        
 
 )H 	tW#JS	2 	2 	2'.D\M	O 	O 	OM J).-  EI).: : : : : :rU   r   c                       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<    e
            ed<   d	S )
cmd_ouz%Organizational Units (OU) management.addcreater   mover;   listlistobjectsN)rG   rH   rI   rJ   subcommandsr_   r   rW   r   r}   rh   rT   rU   rC   r   r     s        //K K#GIIK&JLLK"(**K&JLLK"(**K!0!2!2KrU   r   )samba.getoptgetoptrO   r1   
samba.authr   samba.netcmdr   r   r   r   samba.samdbr   sambar	   operatorr
   r   rW   r_   rh   r}   r   r   rT   rU   rC   <module>r      s  &       



 % % % % % %                             =H =H =H =H =H =H =H =H@A> A> A> A> A>w A> A> A>H/8 /8 /8 /8 /8g /8 /8 /8dC. C. C. C. C.g C. C. C.L72 72 72 72 72w 72 72 72t:: :: :: :: :: :: :: ::z
3 
3 
3 
3 
3\ 
3 
3 
3 
3 
3rU   