
    Id79                         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  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y)    N)system_session)CommandCommandErrorOptionSuperCommand)SamDB)
attrgetterc                       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y)
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                    |j                         }|j                  |d      }t        |t               ||      }		 |	j	                  |      }
	 |	j	                  |      }	 |	j                  |
dt        j                  g       }t        |      d	k(  r| j                  j                  d
|z         y |	j                  |
|       | j                  j                  d|
d|d       y # t
        $ r}t        d|d|      d }~ww xY w# t
        $ r}t        d|d|      d }~ww xY w# t
        $ r}t        d|
z  |      d }~ww xY w)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   normalize_dn_in_domain	Exceptionr   searchldb
SCOPE_BASElenoutfwriterename)selfr   r   r   r   r   r   r#   credssamdbfull_old_ou_dnefull_new_ou_dnress                 1/usr/lib/python3/dist-packages/samba/netcmd/ou.pyrunzcmd_rename.run?   sU   ##%((d(C!.*:"'B0	/"99)DN	/"99)DN

	O,,N*L%(^^2  ?C 3x1}		 :Y FGLL8 			~7EG 	H)  	/ )1 . / /	/
  	/ )1 . / /	/  	O9NJANN	OsN   C% D  AD) 0D) %	D.C??D	D&D!!D&)	E2EENNNN__name__
__module____qualname____doc__synopsisr   strtakes_options
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsrA        r@   r   r   !   sl     9H 	tW#JS	2M
 {+J))..-- BF $HrR   r   c                       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y)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                 ,   |j                         }|j                  |d      }t        |t               ||      }		 |	j	                  |      }
	 |	j	                  |      }t        j                  |	t        |
            }|j                  t        |
      dz
         |j                  |       	 |	j                  |
dt        j                  g 	      }t        |      d
k(  r| j                  j!                  d|
z         y |	j#                  |
|       | j                  j!                  d|
d|d       y # t
        $ r}t        d|d|      d }~ww xY w# t
        $ r}t        d|d|      d }~ww xY w# t
        $ r}t        d|
z  |      d }~ww xY w)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.   r/   r   r   r0   r1   r   r3   DnrI   remove_base_componentsr5   add_baser2   r4   r6   r7   r8   )r9   r   rU   r   r   r   r   r#   r:   r;   r<   r=   full_new_parent_dnr>   r?   s                  r@   rA   zcmd_move.run   s   ##%((d(C!.*:"'B0	/"99)DN	3!&!=!=m!L
 s>':;--c..AA.EF 23		M,,N*L%(^^2  ?C 3x1}		 :^ KLLL8 			');= 	>/  	/ )1 . / /	/
  	3 -q 2 3 3	3   	M7.H!LL	MsN   D2 E -AE6 =E6 2	E;EE	E3E..E36	F?FFrB   rC   rQ   rR   r@   rT   rT   `   sk      =H 	tW#JS	2M
 /J))..-- FJ $!>rR   rT   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y)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                    |j                         }|j                  |d      }t        |t               ||      }		 |	j	                  |      }
	 |	j                  |
|       | j                  j                  d|
z         y # t
        $ r}t        d|d|      d }~ww xY w# t
        $ r}t        d|
z  |      d }~ww xY w)	NTr   r   Invalid ou_dn "r%   )r_   zFailed to add ou "%s"zAdded ou "%s"
)
r.   r/   r   r   r0   r1   r   	create_our6   r7   )r9   r`   r   r   r   r   r_   r#   r:   r;   
full_ou_dnr=   s               r@   rA   zcmd_add.run   s    ##%((d(C!.*:"'B0	F55e<J	HOOJKO@ 			)J67  	F5!DEE	F
  	H6CQGG	Hs/   B  B"  	B	BB"	B?+B::B?)NNNNNrC   rQ   rR   r@   r]   r]      sz     )H 	tW#JS	2%7m	-M J))..-- EI $8rR   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y)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.r`   r   Nc                 ,   |j                         }|j                  |d      }	t        |t               |	|      }
t	        j
                  |
|
j                               }	 |
j                  |      }d}t        j                  }|rd}t        j                  }	 |
j                  |d|g 	      }t        |      |k  r| j                  j                  d
|z         y t!        |t#        d            D ]`  }|j$                  |k(  r|s$|j$                  j'                  t        |             | j                  j                  d|j$                  z         b y # t        $ r}t        d|d|      d }~ww xY w# t        $ r}t        d|z  |      d }~ww xY w)NTr   r   rb   r%   r   rW   z(objectclass=*)r'   zou "%s" is empty
dnkey%s
z"Failed to list contents of ou "%s")r.   r/   r   r   r3   rX   	domain_dnr0   r1   r   SCOPE_ONELEVELSCOPE_SUBTREEr2   r5   r6   r7   sortedr	   rp   rY   )r9   r`   r   r   r   r   rh   rn   r#   r:   r;   rt   rd   r=   minchildrenr*   childrenchilds                     r@   rA   zcmd_listobjects.run   s   ##%((d(C!.*:"'B0FF5%//"34		F55e<J ""K%%E	.||/@*/r $ ;H 8}+		 4u <=j.>? 388z)HH33C	NC		 123#  	F5!DEE	F0  	.C) *+,. .	.s8    E AE6 A9E6 	E3E..E36	F?FF)NNNNFFrC   rQ   rR   r@   rf   rf      s     )H 	tW#JS	2{E,8	:t]e")D	FM J))..-- EI-2$.rR   rf   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y)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   rg   rh   Fri   rj   rk   r   Nc                 v   |j                         }|j                  |d      }t        |t               ||      }	t	        j
                  |	|	j                               }
|r|	j                  |      }
|	j                  |
t        j                  dg       }t        |      dk(  ry t        |t        d            D ]}  }|sHt	        j
                  |	|	j                               }|j                  j                  t        |             | j                  j!                  d	t#        |j                        z          y )
NTr   r   z (objectClass=organizationalUnit))r*   r)   r+   r   rp   rq   rs   )r.   r/   r   r   r3   rX   rt   r0   r2   rv   r5   rw   r	   rp   rY   r6   r7   rI   )r9   r   r   r   r   base_dnrh   r#   r:   r;   	search_dnr?   msgrt   s                 r@   rA   zcmd_list.run7  s    ##%((d(C!.*:"'B0 FF5%//"34	44W=Ill9!$!2!2&H!#  % HM#:d#34 	2CFF5%//*;<	--c)n=IIOOFS[01		2rR   )NNNNNF)rD   rE   rF   rG   rH   r   rI   rJ   rL   rM   rN   rO   rP   rA   rQ   rR   r@   r|   r|     s     !H 	tW#JS	2t[-	 	{E,8	:M ))..-- 2rR   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y)
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_deleteFri   z7Delete organizational unit and all children recursivelyrk   r`   r   Nc                 0   |j                         }|j                  |d      }t        |t               ||      }		 |	j	                  |      }
g }|rdg}	 |	j                  |
dt        j                  g       }t        |      d	k(  r| j                  j                  d
|z         y |	j                  |
|       | j                  j                  d|
z         y # t
        $ r}t        d|d|      d }~ww xY w# t
        $ r}t        d|
z  |      d }~ww xY w)NTr   r   rb   r%   ztree_delete:1r&   r'   r   r,   zFailed to delete ou "%s"zDeleted ou "%s"
)r.   r/   r   r   r0   r1   r   r2   r3   r4   r5   r6   r7   delete)r9   r`   r   r   r   r   r   r#   r:   r;   rd   r=   controlsr?   s                 r@   rA   zcmd_delete.runs  s    ##%((d(C!.*:"'B0	F55e<J '(H		K,,J*L%(^^2  ?C 3x1}		 :U BCLLX. 			+j89%  	F5!DEE	F  	K9JFJJ	Ks6   C AC8 %C8 	C5C00C58	DDD)NNNNFrC   rQ   rR   r@   r   r   U  s    
 )H 	tW#JS	2'.D\M	OM J))..-- EI).:rR   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<   y	)
cmd_ouz%Organizational Units (OU) management.addcreater   mover8   listlistobjectsN)rD   rE   rF   rG   subcommandsr]   r   rT   r   r|   rf   rQ   rR   r@   r   r     s_    /K K#IK&LK"*K&LK"*K!0!2KrR   r   )samba.getoptgetoptrL   r3   
samba.authr   samba.netcmdr   r   r   r   samba.samdbr   operatorr	   r   rT   r]   rf   r|   r   r   rQ   rR   r@   <module>r      s   &  
 %   <H <H~@>w @>F/8g /8dC.g C.L72w 72t9: 9:x
3\ 
3rR   