
    g(                     b    S SK Jr  S SKJrJr  S SKJr  S SKJ	r	J
r
Jr  S SKJr   " S S\	5      rg)    N)dsdbldb)system_session)CommandCommandErrorOption)SamDBc                      \ rS rSrSrSr\" SSS\SSS	9\" S
S\S9\" SS\S9\" SS\S9\" SS\SS9\" SSSS9\" SS\S9\" SS\S9\" SS\S9\" SS \S9/
rS!/r	\
R                  \
R                  \
R                  S".r     S'S$ jrS% rS&rg#)(cmd_user_rename   a%  Rename a user and related attributes.

This command allows to set the user's name related attributes. The user's
CN will be renamed automatically.
The user's new CN will be made up by combining the given-name, initials
and surname. A dot ('.') will be appended to the initials automatically
if required.
Use the --force-new-cn option to specify the new CN manually and the
--reset-cn option to reset this change.

Use an empty attribute value to remove the specified attribute.

The username specified on the command is the sAMAccountName.

The command may be run locally from the root userid or another authorized
userid.

The -H or --URL= option can be used to execute the command against a remote
server.

Example1:
samba-tool user rename johndoe --surname='Bloggs'

Example1 shows how to change the surname of a user 'johndoe' to 'Bloggs' on
the local server. The user's CN will be renamed automatically, based on
the given name, initials and surname.

Example2:
samba-tool user rename johndoe --force-new-cn='John Bloggs (Sales)' \
    --surname=Bloggs -H ldap://samba.samdom.example.com -U administrator

Example2 shows how to rename the CN of a user 'johndoe' to 'John Bloggs (Sales)'.
Additionally the surname ('sn' attribute) is set to 'Bloggs'.
The -H parameter is used to specify the remote target server.
z%prog <username> [options]z-Hz--URLz%LDB URL for database or target serverURLH)helptypemetavardestz	--surnamezNew surname)r   r   z--given-namezNew given namez
--initialszNew initialsz--force-new-cnz^Specify a new CN (RDN) instead of using a combination of the given name, initials and surname.NEW_CN)r   r   r   z
--reset-cnzSet the CN (RDN) to the combination of the given name, initials and surname. Use this option to reset the changes made with the --force-new-cn option.
store_true)r   actionz--display-namezNew display namez--mail-addresszNew email addressz--samaccountnamez,New account name (sAMAccountName/logon name)z--upnzNew user principal nameusername)	sambaoptscredoptsversionoptsNc           	         U(       a  U(       a  [        S5      eUS:X  a  [        S5      eUS:X  a  [        S5      eUR                  5       nUR                  USS9n[        U[	        5       UUS9n[
        R                  " UUR                  5       5      nS[        R                  [
        R                  " U5      4-  n UR                  UU[
        R                  / S	QS
9nUS   nUR                  nUR                  5       nUS   S   nUb  UOUS   nUb  UnOUR!                  UUUUUS9nUR!                  UUS   S9n[#        U5      [#        U5      :g  =(       a2    [#        U5      [#        U5      :H  =(       d    U=(       d    [%        U5      n[
        R                  " USU-  5      nUR'                  U5        Ub$  U R)                  UU5      S:X  a  [        SU-  5      e[
        R*                  " 5       nUUl        UR-                  UUSU5        UR-                  UUSU5        UR-                  UUSU5        UR-                  UUSU	5        UR-                  UUSU
5        UR-                  UUSU5        UR-                  UUSU5        [/        U5      S:  nUR1                  5          US:X  a  UR3                  U5        US:X  a  UR5                  UU5        UR;                  5         US:X  a(  U R<                  R?                  SU< SU< SU< S35        US:X  ap  U R<                  R?                  SU-  5        URA                  5        H=  n!U!S :X  a  M  U R<                  R?                  U!< S!UU!   (       a  UU!   OS"< S#35        M?     g g ! [         a    [        SU-  5      ef = f! [6         a$  n UR9                  5         [        SU-  U 5      eS n A ff = f)$NzEIt is not allowed to specify --force-new-cn together with --reset-cn. z7Failed to rename user - delete protected attribute 'CN'zCFailed to rename user - delete protected attribute 'sAMAccountName'T)fallback_machine)urlsession_infocredentialslpz)(&(sAMAccountType=%d)(sAMAccountName=%s)))sAMAccountName	givenNameinitialssnmailuserPrincipalNamedisplayNamecn)base
expressionscopeattrsr   zUnable to find user "%s"r(   r!   )	old_attrs
given_namer#   surnamefallback_default)r-   r0   zCN=%sFzo"%s" is not a valid upn. You can manage the upn suffixes with the "samba-tool domain trust namespaces" command.r"   r#   r$   r'   r%   r&   zFailed to rename user "%s"zRenamed CN of user "z" from "z" to "z" successfully
zBFollowing attributes of user "%s" have been changed successfully:
dnz: z	[removed]
)!r   get_loadparmget_credentialsr	   r   r   Dn	domain_dnr   ATYPE_NORMAL_ACCOUNTbinary_encodesearchSCOPE_SUBTREEr1   
IndexErrorparentfullname_from_namesstrbooladd_baseis_valid_upnMessageprepare_attr_replacelentransaction_startmodifyrename	Exceptiontransaction_canceltransaction_commitoutfwritekeys)"selfr   r   r   r   r   r/   r.   r#   display_namemail_addresssamaccountnameupnforce_new_cnreset_cnr    credssamdbr6   filterresold_useruser_dnuser_parent_dnold_cnnew_fallback_cnnew_user_cnexpected_cnmust_change_cnnew_user_dn
user_attrsattributes_changedeattrs"                                     :/usr/lib/python3/dist-packages/samba/netcmd/user/rename.pyruncmd_user_rename.runo   s%    H  ; < <2  0 1 1R  < = = ##%((d(C!.*:"'B0FF5%//"34	=,,c.?.?.IJK	H,,I*0%(%6%6&,  
-C 1vHkkG !)$" -;,F.*+ 	 #&K33h?I=E<CET	 4 VK //(AIJZA[ 0 ]VK(88 ,K3{++ ++\* 	 ffUGk$9:^,?  ,5" $@ CF$F G G
 [[]

"":xjQ"":xXN"":xwG"":xU"":xN"":x9I>Z"":x9LcR _q0!	K!T)Z(%Wk2 	  "T!IIOO08&+O P %IIOO 69AC D")DL		d#-d#3 5?t4D9D5E !F G * &A  	H9XFGG	Hn  	K$$&;hFJJ	Ks$   ?3N )/N) N&)
O3OOc                    UR                  5       nUR                  5       nX4/nUR                  5       nSU-  nUR                  U[        R
                  SS/S9n[        U5      S:  a?  US   n	SU	;   a4  U	S    H+  n
UR                  [        U
5      R                  5       5        M-     UR                  S5      n[        U5      S:  a  g	US
   R                  5       nX;  a  g	g)NzCN=Partitions,%sz(objectClass=crossRefContainer)uPNSuffixes)r)   r+   r*   r,      r   @   FT)domain_dns_nameforest_dns_nameget_config_basednr9   r   
SCOPE_BASErD   appendr>   lowersplit)rN   rV   rR   
domain_dns
forest_dnsupn_suffixesconfig_basednpartitions_dnrX   msgs	upn_split
upn_suffixs                rf   rA   cmd_user_rename.is_valid_upn   s    **,
**,
"///1*]:ll..8 /	  # HMa&C#]+A ''A7 , IIcN		NQr]((*
)     )NNNNNNNNNNNNN)__name__
__module____qualname____firstlineno____doc__synopsisr   r>   takes_options
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsrg   rA   __static_attributes__r   r   rf   r   r      s(   "H ,H 	tW;S	2 	{!	 	~$	 	|"	 	?	+ 	|G #		$
 	&	 	'	 	!B	 	w-	?"MH J))..-- 6:?C;?8<	mG^r   r   )samba.getoptgetoptr   sambar   r   
samba.authr   samba.netcmdr   r   r   samba.samdbr	   r   r   r   rf   <module>r      s'   ,   % 6 6 \g \r   