
    gy                     X   S SK Jr  S SKrS SKrS SKrS SKJrJr  S SK	J
r
  S SKJr  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Jr  S S	KJr  S
SKJr   " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S\5      r# " S S\5      r$g)    N)
check_callCalledProcessError)
attrgetter)system_session)SamDB)credentialsdsdb)Net)CommandCommandErrorSuperCommandOption)	get_bytes   )commonc                   v   \ 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9\" SS\S9\" SS\S9\" SS\S9\" SS\S9\" SS\S9\" 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g))-cmd_add+   af  Add a new contact.

This command adds a new contact to the Active Directory domain.

The name of the new contact can be specified by the first argument
'contactname' or the --given-name, --initial and --surname arguments.
If no 'contactname' is given, contact's name will be made up of the given
arguments by combining the given-name, initials and surname. Each argument
is optional. A dot ('.') will be appended to the initials automatically.

Example1:
samba-tool contact add "James T. Kirk" --job-title=Captain \
    -H ldap://samba.samdom.example.com -UAdministrator%Passw1rd

The example shows how to add a new contact to the domain against a remote
LDAP server.

Example2:
samba-tool contact add --given-name=James --initials=T --surname=Kirk

The example shows how to add a new contact to the domain against a local
server. The resulting name is "James T. Kirk".
z%prog [contactname] [options]-H--URL%LDB URL for database or target serverURLHhelptypemetavardestz--ouzDN of alternative location (with or without domainDN counterpart) in which the new contact will be created. E.g. 'OU=<OU name>'. Default is the domain base.r   r   	--surnamezContact's surname--given-namezContact's given name
--initialszContact's initials--display-namezContact's display namez--job-titlezContact's job titlez--departmentzContact's departmentz	--companyzContact's companyz--descriptionzContact's description--mail-addresszContact's email addressz--internet-addresszContact's home pagez--telephone-numberzContact's phone numberz--mobile-numberzContact's mobile phone numberz--physical-delivery-officezContact's office locationzfullcontactname?	sambaoptscredoptsversionoptsNc                 "   UR                  5       nUR                  U5      n [        U[        5       UUS9nUR	                  UUUUU	U
UUUUUUUUUS9nU R                  R                  SU-  5        g ! [
         a  n[        SU5      eS nAff = f)Nurlsession_infor   lp)fullcontactnameousurname	givennameinitialsdisplaynamejobtitle
departmentcompanydescriptionmailaddressinternetaddresstelephonenumbermobilenumberphysicaldeliveryofficezFailed to add contactz Contact '%s' added successfully
)	get_loadparmget_credentialsr   r   
newcontact	Exceptionr   outfwrite)selfr.   r&   r'   r(   r   r/   r0   
given_namer2   display_name	job_titler5   r6   r7   mail_addressinternet_addresstelephone_numbermobile_numberphysical_delivery_officer-   credssamdbret_namees                            6/usr/lib/python3/dist-packages/samba/netcmd/contact.pyruncmd_add.runj   s    * ##%((,	;a'5'7&+!E '' /$!("%'( 0 0*'? ( AH& 			;hFG  	;6::	;s   1A3 3
B=B		B )NNNNNNNNNNNNNNNNNNN__name__
__module____qualname____firstlineno____doc__synopsisr   strtakes_options
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsrQ   __static_attributes__rS       rP   r   r   +   sj   0 /H 	tW#JS	2v3 	 	{!43?~$:E|"6SA&>SI}#8sC~$:E{!43?%<3G&?cJ#*?cJ#*BM 3	 	+/	/M8 %%J ))..-- !!!%)'0Hrd   r   c            	           \ rS rSrSrSr\" SSS\SSS	9/rS
/r	\
R                  \
R                  \
R                  S.r    SS jrSrg)
cmd_delete   a  Delete a contact.

This command deletes a contact object from the Active Directory domain.

The contactname specified on the command is the common name or the
distinguished name of the contact object. The distinguished name of the
contact can be specified with or without the domainDN component.

Example:
samba-tool contact delete Contact1 \
    -H ldap://samba.samdom.example.com \
    --username=Administrator --password=Passw1rd

The example shows how to delete a contact in the domain against a remote
LDAP server.
%prog <contactname> [options]r   r   r   r   r   r   contactnamer%   Nc                    UR                  5       nUR                  USS9n[        U[        5       UUS9nUR	                  5       n	[
        R                  n
S[
        R                  " U5      -  nUR                  5       R                  S5      (       a$  Sn[
        R                  n
 UR                  U5      n	 UR                  U	U
US	/S
9nUS   R                  n[#        U5      S:  aP  [%        U['        S	5      S9 H+  nU R(                  R+                  SUR                  -  5        M-     [        SU-  5      e UR-                  U5        U R(                  R+                  SU-  5        g ! [         a  n[        SU< SU< 35      eS nAff = f! [          a    [        SU-  5      ef = f! [         a  n[        SU-  U5      eS nAff = f)NTfallback_machiner*   !(&(objectClass=contact)(name=%s))CN=(objectClass=contact)Invalid dn "": dnbasescope
expressionattrsr   Unable to find contact "%s"r   key
found: %s
FMultiple results for contact '%s'
Please specify the contact's full DNzFailed to remove contact "%s"zDeleted contact %s
)r=   r>   r   r   	domain_dnldbSCOPE_SUBTREEbinary_encodeupper
startswith
SCOPE_BASEnormalize_dn_in_domainr@   r   searchrr   
IndexErrorlensortedr   rA   rB   delete)rC   ri   r&   r'   r(   r   r-   rL   rM   base_dnru   filterrO   res
contact_dnmsgs                   rP   rQ   cmd_delete.run   s    ##%((d(C!#1#3"' //#!!5##K01 ))%00,FNNE566{C
	N,,G%**0&*V  -C QJ s8a<cz$'78		 67 9  F* + , ,	QLL$ 			.<=1  5"$/$4 5 55  	N<LMM	N  	Q>LaPP	Qs<   %E) 7"F 9F* )
F3FFF'*
G4GGrS   NNNNrT   rS   rd   rP   rf   rf      sq      /H 	t;	M  J ))..-- 0>rd   rf   c            
           \ rS rSrSrSr\" SSS\SSS	9\" S
SS\S9\" SSSSSS9/r\	R                  \	R                  \	R                  S.r      SS jrSrg)cmd_list   zList all contacts.
    z%prog [options]r   r   r   r   r   r   z-bz	--base-dnzSpecify base DN to use.r   z	--full-dnfull_dnF
store_truez.Display contact's full DN instead of the name.)r   defaultactionr   r%   Nc                     UR                  5       nUR                  USS9n[        U[        5       UUS9n	U	R	                  5       n
U(       a  U	R                  U5      n
U	R                  U
[        R                  SS/S9n[        U5      S:X  a  g U(       aC  [        U[        S5      S	9 H+  nU R                  R                  S
UR                  -  5        M-     g U H1  nUR                  SSS9nU R                  R                  S
U-  5        M3     g )NTrk   r*   ro   name)ru   rv   rw   r   rr   ry   z%s
)idx)r=   r>   r   r   r}   r   r   r~   r   r   r   r   rA   rB   rr   get)rC   r&   r'   r(   r   r   r   r-   rL   rM   	search_dnr   r   contact_names                 rP   rQ   cmd_list.run  s     ##%((d(C!#1#3"'
 OO%	44W=Ill9!$!2!2&="(  + HMcz$'78		0 9C776q71LIIOOF\12 rd   rS   )NNNNNF)rU   rV   rW   rX   rY   rZ   r   r[   r\   r^   r_   r`   ra   rb   rQ   rc   rS   rd   rP   r   r      s     !H 	t;	 	t[-	 	{"D		FM$ ))..-- "3rd   r   c            	           \ rS rSrSrSr\" SSS\SSS	9\" S
S\S9/rS/r	\
R                  \
R                  \
R                  S.r     SS jrSrg)cmd_editi6  a  Modify a contact.

This command will allow editing of a contact object in the Active Directory
domain. You will then be able to add or change attributes and their values.

The contactname specified on the command is the common name or the
distinguished name of the contact object. The distinguished name of the
contact can be specified with or without the domainDN component.

The command may be run 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 contact edit Contact1 -H ldap://samba.samdom.example.com \
    -U Administrator --password=Passw1rd

Example1 shows how to edit a contact's attributes in the domain against a
remote LDAP server.

The -H parameter is used to specify the remote target server.

Example2:
samba-tool contact edit CN=Contact2,OU=people,DC=samdom,DC=example,DC=com

Example2 shows how to edit a contact's attributes in the domain against a
local server. The contact, which is located in the 'people' OU,
is specified by the full distinguished name.

Example3:
samba-tool contact edit Contact3 --editor=nano

Example3 shows how to edit a contact's attributes in the domain against a
local server using the 'nano' editor.
rh   r   r   r   r   r   r   z--editorzQEditor to use instead of the system default, or 'vi' if no system default is set.r   ri   r%   Nc                 v   UR                  5       nUR                  USS9n[        U[        5       XS9n	U	R	                  5       n
[
        R                  nS[
        R                  " U5      -  nUR                  5       R                  S5      (       a$  Sn[
        R                  n U	R                  U5      n
 U	R                  U
UUS	9nUS
   R                  n[#        U5      S:  aP  [%        U['        S5      S9 H+  nU R(                  R+                  SUR                  -  5        M-     [        SU-  5      eU H  n[,        R.                  " U	U5      nUc$  [0        R2                  R5                  S5      nUc  Sn[6        R8                  " SS9 nUR+                  [;        U5      5        UR=                  5          [?        UUR@                  /5        [E        UR@                  5       nURG                  5       nS S S 5        S S S 5        M     U	RI                  W5      n[K        U5      S   nU	RM                  WU5      n[#        U5      S
:X  a  U R(                  R+                  S5        g  U	RO                  U5        U R(                  R+                  SU-  5        g ! [         a  n[        SU< SU< 35      eS nAff = f! [          a    [        SU-  5      ef = f! [B         a  n[C        SU5      eS nAff = f! , (       d  f       GN= f! , (       d  f       GM  = f! [         a  n[        SU-  U5      eS nAff = f)NTrk   r*   rm   rn   ro   rp   rq   )rt   ru   rv   r   rx   r   rr   ry   r{   r|   EDITORviz.tmp)suffixzERROR: zNothing to do
zFailed to modify contact '%s': z#Modified contact '%s' successfully
)(r=   r>   r   r   r}   r~   r   r   r   r   r   r   r@   r   r   rr   r   r   r   r   rA   rB   r   get_ldif_for_editorosenvironr   tempfileNamedTemporaryFiler   flushr   r   r   openread
parse_ldifnextmsg_diffmodify)rC   ri   r&   r'   r(   r   editorr-   rL   rM   r   ru   r   rO   r   r   r   result_ldift_fileedited_fileedited_messagemsgs_edited
msg_editedres_msg_diffs                           rP   rQ   cmd_edit.runq  s    ##%((d(C!.*:"'0//#!!5$$[12 ))%00,FNNE566{C
	N,,G%**0  2C QJ s8a<cz$'78		 67 9  F* + , , C 44UC@K~1>!F,,F;vY{34;45 &++&+%0%5%5%7N ' <; & &&~6+&q)
~~c:6|!IIOO-.	"LL&
 			>LMi  5"$/$4 5 55  	N<LMM	N. * ;,Y::;&& <;*  	"@;N " "	"s   $J 6 J; +L8KL$K55L&L 
J8 J33J8;K
K2!K--K22L5
L?L
L	
L8$L33L8rS   NNNNNrT   rS   rd   rP   r   r   6  s    #H /H 	t;	 	z;	M  J))..-- KNrd   r   c            	           \ rS rSrSrSr\" SSS\SSS	9\" S
S\SS9/rS/r	\
R                  \
R                  \
R                  S.r     SS jrSrg)cmd_showi  a  Display a contact.

This command displays a contact object with it's attributes in the Active
Directory domain.

The contactname specified on the command is the common name or the
distinguished name of the contact object. The distinguished name of the
contact can be specified with or without the domainDN component.

The command may be run 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 contact show Contact1 -H ldap://samba.samdom.example.com \
    -U Administrator --password=Passw1rd

Example1 shows how to display a contact's attributes in the domain against
a remote LDAP server.

The -H parameter is used to specify the remote target server.

Example2:
samba-tool contact show CN=Contact2,OU=people,DC=samdom,DC=example,DC=com

Example2 shows how to display a contact's attributes in the domain against
a local server. The contact, which is located in the 'people' OU, is
specified by the full distinguished name.

Example3:
samba-tool contact show Contact3 --attributes=mail,mobile

Example3 shows how to display a contact's mail and mobile attributes.
rh   r   r   r   r   r   r   z--attributesz:Comma separated list of attributes, which will be printed.contact_attrs)r   r   r   ri   r%   Nc                    UR                  5       nUR                  USS9n[        U[        5       UUS9n	U	R	                  5       n
[
        R                  nS nU(       a  UR                  S5      nS[
        R                  " U5      -  nUR                  5       R                  S5      (       a$  Sn[
        R                  n U	R                  U5      n
 U	R                  U
UUUS
9nUS   R                   n[%        U5      S:  aP  ['        U[)        S5      S9 H+  nU R*                  R-                  SUR                   -  5        M-     [        SU-  5      eU H5  n[.        R0                  " U	U5      nU R*                  R-                  U5        M7     g ! [         a  n[        SU< S	U< 35      eS nAff = f! ["         a    [        SU-  5      ef = f)NTrk   r*   ,rm   rn   ro   rp   rq   )rt   rv   ru   rw   r   rx   r   rr   ry   r{   zAMultiple results for contact '%s'
Please specify the contact's DN)r=   r>   r   r   r}   r~   r   splitr   r   r   r   r   r@   r   r   rr   r   r   r   r   rA   rB   r   r   )rC   ri   r&   r'   r(   r   r   r-   rL   rM   r   ru   rw   r   rO   r   r   r   contact_ldifs                      rP   rQ   cmd_show.run  s    ##%((d(C!#1#3"' //#!!!'',E5##K01 ))%00,FNNE566{C
	N,,G*0%*%*  ,C QJ s8a<cz$'78		 67 9  A* + , , C!55eSALIIOOL) )  5"$/$4 5 55  	N<LMM	Ns$   ?F !F2 
F/F**F/2GrS   r   rT   rS   rd   rP   r   r     s    "F /H 	t;	 	~.#		%M  J))..-- 4*rd   r   c            	           \ rS rSrSrSr\" SSS\SSS	9/rS
S/r	\
R                  \
R                  \
R                  S.r    SS jrSrg)cmd_movei1  a  Move a contact object to an organizational unit or container.

The contactname specified on the command is the common name or the
distinguished name of the contact object. The distinguished name of the
contact can be specified with or without the domainDN component.

The name of the organizational unit or container can be specified as the
distinguished name, with or without the domainDN component.

The command may be run 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 contact move Contact1 'OU=people' \
    -H ldap://samba.samdom.example.com -U Administrator

Example1 shows how to move a contact Contact1 into the 'people'
organizational unit on a remote LDAP server.

The -H parameter is used to specify the remote target server.

Example2:
samba-tool contact move Contact1 OU=Contacts,DC=samdom,DC=example,DC=com

Example2 shows how to move a contact Contact1 into the OU=Contacts
organizational unit on the local server.
z-%prog <contactname> <new_parent_dn> [options]r   r   r   r   r   r   ri   new_parent_dnr%   Nc                    UR                  5       nUR                  USS9n[        U[        5       UUS9n	U	R	                  5       n
[
        R                  nS[
        R                  " U5      -  nUR                  5       R                  S5      (       a$  Sn[
        R                  n U	R                  U5      n
 U	R                  U
UUS	/S
9nUS   R                  n[#        U5      S:  aP  [%        U['        S	5      S9 H+  nU R(                  R+                  SUR                  -  5        M-     [        SU-  5      e U	R                  U5      n[
        R,                  " U	[/        U5      5      nUR1                  [#        U5      S-
  5        UR3                  U5         U	R5                  UU5        U R(                  R+                  SU< SU< S35        g ! [         a  n[        SU< SU< 35      eS nAff = f! [          a    [        SU-  5      ef = f! [         a  n[        SU< SU< 35      eS nAff = f! [         a  n[        SU-  U5      eS nAff = f)NTrk   r*   rm   rn   ro   rp   rq   rr   rs   r   rx   r   ry   r{   r|   zInvalid new_parent_dn "zFailed to move contact "%s"zMoved contact "z" into "z"
)r=   r>   r   r   r}   r~   r   r   r   r   r   r   r@   r   r   rr   r   r   r   r   rA   rB   Dnr[   remove_base_componentsadd_baserename)rC   ri   r   r&   r'   r(   r   r-   rL   rM   r   ru   r   rO   r   r   r   full_new_parent_dnfull_new_contact_dns                      rP   rQ   cmd_move.runb  s[    ##%((d(C!#1#3"' //#!!5##K01 ))%00,FNNE566{C
	N,,G%**0&*V  -C QJ s8a<cz$'78		 67 9  F* + , ,	3!&!=!=m!L
 "ffUC
O<223z?Q3FG$$%78	OLL%89 			$&8: 	;E  5"$/$4 5 55  	N<LMM	N  	3 -q 2 3 3	3  	O<{JANN	OsT   %G 7"G5 9H H6 
G2G--G25H
H3H..H36
I IIrS   r   rT   rS   rd   rP   r   r   1  sr    < ?H 	t;	M  1J))..-- <;rd   r   c                       \ 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/rS/r	\
R                  \
R                  \
R                  S.r   S"S  jrS!rg)#
cmd_renamei  a5  Rename a contact and related attributes.

This command allows to set the contact's name related attributes.
The contact'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 changes.

Use an empty attribute value to remove the specified attribute.

The contactname specified on the command is the CN.

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 contact rename "John Doe" --surname=Bloggs \
    --force-new-cn=John

Example1 shows how to change the surname ('sn' attribute) of a contact
'John Doe' to 'Bloggs' and change the CN to 'John' on the local server.

Example2:
samba-tool contact rename "J Doe" --given-name=John
    -H ldap://samba.samdom.example.com -U administrator

Example2 shows how to rename the given name of a contact 'J Doe' to
'John'. The contact's cn will be renamed automatically, based on
the given name, initials and surname, if the previous CN is the
standard combination of the previous name attributes.
The -H parameter is used to specify the remote target server.
rh   r   r   r   r   r   r   r    zNew surnamer   r!   zNew given namer"   z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.r   )r   r   r#   zNew display namer$   zNew email addressri   r%   Nc           	         U	(       a  U(       a  [        S5      eU	S:X  a  [        S5      eUR                  5       nUR                  USS9n[        U[	        5       XS9n[
        R                  " XR                  5       5      nS[
        R                  " U5      -  n UR                  U[
        R                  U/ SQS	9nUS
   nUR                  nUR                  5       nUS   S
   nU	b  U	nOUR                  UUUUS9nUR                  U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S:X  a  U(       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5      S
:  nUR+                  5          US:X  a  UR-                  U5        U(       a  UR/                  UU5        UR5                  5         U(       a(  U R6                  R9                  SU< SU< SU< S35        U(       ap  U R6                  R9                  SU-  5        UR;                  5        H=  nUS:X  a  M  U R6                  R9                  U< SUU   (       a  UU   OS< S35        M?     g g ! [         a    [        SU-  5      ef = f! [0         a$  nUR3                  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. z:Failed to rename contact - delete protected attribute 'CN'Trk   r*   rm   )r   sn	givenNamecnr2   displayNamemailrs   r   rx   r   )	old_attrsrD   r2   r0   )r   zCN=%szFailed to rename contact '%s' - can not set an empty CN (please use --force-new-cn to specify a different CN or --given-name, --initials or --surname to set name attributes)r   r   r2   r   r   zFailed to rename contact "%s"zRenamed CN of contact "z" from "z" to "z" successfully
zEFollowing attributes of contact "%s" have been changed successfully:
rr   z: z	[removed]
)r   r=   r>   r   r   r~   r   r}   r   r   r   rr   r   parentfullname_from_namesr[   boolr   Messageprepare_attr_replacer   transaction_startr   r   r@   transaction_canceltransaction_commitrA   rB   keys)rC   ri   r'   r&   r(   r   r0   rD   r2   force_new_cnrE   rG   reset_cnr-   rL   rM   r}   r   r   old_contactr   contact_parent_dnold_cnnew_cnexcepted_cnmust_change_cnnew_contact_dnr   contact_attributes_changedrO   attrs                                  rP   rQ   cmd_rename.run  s    H  ; < <2  0 1 1 ##%((d(C!.*:"'0FF5//"34	5##K01	M,,I%(%6%6*0&.  
C a&K$J '--/T"1%#!F..:D8@7> / @F //+/FVF3 :f+[)99 9"9&*<&8 	 w'78 12R<N  C FL	 L M M %""=+{JW""=+tWM""=+z8T""=+}l[""=+v|T%(%7!%;" 	!	Q)T1]+Z8 	  "IIOO0;VVM N &IIOO 69DF G%**,4<		d,T2 5B$4G8C5D !E F - &u  	M;{KLL	Mb  	Q$$&>LaPP	Qs$   3L* 0M *M
M4M//M4rS   )NNNNNNNNNNNrT   rS   rd   rP   r   r     s    #J /H 	tW8e#	/ 	{!	 	~$	 	|"	 	?	+ 	|G #		$
 	&	 	'	3
M<  J))..-- KOOS;?bFrd   r   c                       \ rS rSrSr0 r\" 5       \S'   \" 5       \S'   \" 5       \S'   \" 5       \S'   \	" 5       \S'   \
" 5       \S'   \" 5       \S	'   \" 5       \S
'   Srg)cmd_contactiR  zContact management.addcreater   editlistmoveshowr   rS   N)rU   rV   rW   rX   rY   subcommandsr   rf   r   r   r   r   r   rc   rS   rd   rP   r   r   R  sj    K K#IK&LK"*K"*K"*K"*K&LKrd   r   )%samba.getoptgetoptr^   r~   r   r   
subprocessr   r   operatorr   
samba.authr   samba.samdbr   sambar   r	   	samba.netr
   samba.netcmdr   r   r   r   samba.commonr   r   r   r   rf   r   r   r   r   r   r   rS   rd   rP   <module>r     s   &  
 	  5  %    # oHg oHdT> T>n?3w ?3DFNw FNRo*w o*dm;w m;^pF pFd), )rd   