
    gd                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	  S SK
JrJrJr  S SKJrJr  S SKJrJrJr  S SK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%J&r&  S SK'J(r(J)r)J*r*J+r+  S!S jr,S r-S r.S r/ " S S\(5      r0 " S S\(5      r1 " S S\(5      r2 " S S\(5      r3 " S S\(5      r4 " S S\(5      r5 " S S \*5      r6g)"    N)sd_utils)	dnsserverdnspsecurity)ARecord
AAAARecord)
ndr_unpackndr_pack	ndr_print)remove_dns_references)system_session)SamDB)	get_bytes)
check_callCalledProcessError   )common)credentialsdsdbLdbwerrorWERRORError)CommandCommandErrorSuperCommandOptionc                     U(       d   [         R                  [         R                  /nU H  n [         R                  " X 5          g   g! [         R                   a     M6  f = f)z Check ip string is valid addressTF)socketAF_INETAF_INET6	inet_ptonerror)	ip_stringaddress_familiesaddress_familys      7/usr/lib/python3/dist-packages/samba/netcmd/computer.py_is_valid_ipr'   8   sW     "NNFOO<*	^7 +  || 		s   A		A! A!c                 4    [        U [        R                  /S9$ )z%Check ip string is valid ipv4 addressr$   )r'   r   r   r#   s    r&   _is_valid_ipv4r+   G   s    	V^^4DEE    c                 4    [        U [        R                  /S9$ )z%Check ip string is valid ipv6 addressr)   )r'   r   r    r*   s    r&   _is_valid_ipv6r.   L   s    	V__4EFFr,   c                    UR                  S5      n[        R                  n[        R                  [        R                  -  nU R                  5       n	Sn
[        R                  " U 5      n UR                  USUU	US[        R                  USS5
      u  pU
(       a  WR                   H  nUR                    Hu  nUR"                  [        R$                  :X  d   UR"                  [        R&                  :X  d  MA  [        R(                  " 5       nUUl         UR+                  USUU	USU5        Mw     M     U H  n[-        U5      (       a)  UR/                  SU< SU	< SU< 35        [1        U5      nOS[3        U5      (       a)  UR/                  S	U< SU	< S
U< 35        [5        U5      nO[7        SR9                  U5      5      e[        R(                  " 5       nUUl        UR+                  USUU	UUS5        M     [;        U5      S:  aw  [<        R>                  " U SU RA                  5       -  5      nU RC                  U< SU	< 3US9u  nnURE                  UUS[F        RH                  [F        RJ                  -  -  /S9  gg! [         a.  nUR                  S   [        R                  :X  a  Sn
 SnAGN!SnAff = f! [         a.  nUR                  S   [        R                  :w  a  e  SnAGM:  SnAff = f)z3Add DNS A or AAAA records while creating computer. $Tr   NFzAdding DNS AAAA record .z for IPv6 IP: zAdding DNS A record z for IPv4 IP: zInvalid IP: {}zDC=DomainDnsZones,%s)dns_partitionzsd_flags:1:%d)controls)&rstripr   DNS_CLIENT_VERSION_LONGHORNDNS_RPC_VIEW_AUTHORITY_DATADNS_RPC_VIEW_NO_CHILDRENdomain_dns_namer   SDUtilsDnssrvEnumRecords2r   DNS_TYPE_ALLr   argsr   "WERR_DNS_ERROR_NAME_DOES_NOT_EXISTrecrecordswType
DNS_TYPE_ADNS_TYPE_AAAADNS_RPC_RECORD_BUFDnssrvUpdateRecord2r.   infor   r+   r   
ValueErrorformatlenldbDnget_default_basedn
dns_lookupmodify_sd_on_dnr   SECINFO_OWNERSECINFO_GROUP)samdbnamedns_connchange_owner_sdserverip_address_listloggerclient_versionselect_flagszone
name_found	sd_helperbuflenreser>   recorddel_rec_buf
ip_addressadd_rec_bufdomaindns_zone_dndns_a_dnldap_records                          r&   add_dns_recordsrf   Q   s    ;;sD::N889;];]]L  "DJ  'I11
  77C++<<4??2flldFXFX6X"+">">"@K&,KO" 44*"   ' & ( &
*%%KKdJ( )Z(CJ''KKdJ( )*%C-44Z@AA  224$$	
! &4 	Oq FF"U%=%=%??

 !& 0 0T"+ !1 !
+ 	!!%)?)?(BXBX)XYZ 	" 	
 	!g  66!9AAAJ* ' "66!9(Q(QQ! R"s0   ,*J J?
J<#J77J<?
K7	"K22K7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S9\" SSSSS9\" SSSSS9/rS/r	\
R                  \
R                  \
R                  S.r   S S jrSrg)!cmd_computer_add   a+  Add a new computer.

This command adds a new computer account to the Active Directory domain.
The computername specified on the command is the sAMaccountName without the
trailing $ (dollar sign).

Computer accounts may represent physical entities, such as workstations. Computer
accounts are also referred to as security principals and are assigned a
security identifier (SID).

Example1:
samba-tool computer add Computer1 -H ldap://samba.samdom.example.com \
    -Uadministrator%passw1rd

Example1 shows how to add a new computer to the domain against a remote LDAP
server. The -H parameter is used to specify the remote target server. The -U
option is used to pass the userid and password authorized to issue the command
remotely.

Example2:
sudo samba-tool computer add Computer2

Example2 shows how to add a new computer to the domain against the local
server. sudo is used so a user may run the command as root.

Example3:
samba-tool computer add Computer3 --computerou='OU=OrgUnit'

Example3 shows how to add a new computer in the OrgUnit organizational unit.

%prog <computername> [options]-H--URL%LDB URL for database or target serverURLHhelptypemetavardestz--computerouzDN of alternative location (with or without domainDN counterpart) to default CN=Computers in which new computer object will be created. E.g. 'OU=<OU name>'rq   rr   z--descriptionzComputer's descriptionz--prepare-oldjoinz5Prepare enabled machine account for oldjoin mechanism
store_true)rq   actionz--ip-addressrU   ziIPv4 address for the computer's A record, or IPv6 address for AAAA record, can be provided multiple timesappend)rt   rq   rw   z--service-principal-nameservice_principal_name_listzAComputer's Service Principal Name, can be provided multiple timescomputername	sambaoptscredoptsversionoptsNc           
         U	c  / n	U
c  / n
U	 H-  n[        U5      (       a  M  [        SR                  U5      5      e   UR                  5       nUR	                  U5      n [        U[        5       XS9nUR                  XUUU	U
S9  U	(       Gas  [        R                  " SSU5      nUR                  S5      (       a  [        SU-  5      eSR                  [        R                  " U5      5      nUR                  UR                  5       [        R                  US	S
/S9nUS   S	   S   n[!        ["        R$                  US   S
   S   5      n[&        R&                  " SR                  UR)                  5       5      X5      n["        R*                  " 5       nUUl        ["        R$                  " SR                  UR/                  5       U5      5      Ul        [3        XUUUR)                  5       XR5                  5       5        U R8                  R;                  SU-  5        g ! [6         a  n[        SU-  U5      eS nAff = f)NzInvalid IP address {}urlsession_infor   lp)
computeroudescriptionprepare_oldjoinrU   ry   z\$$ r0   zIllegal computername "%s"z-(&(sAMAccountName={}$)(objectclass=computer))primaryGroupID	objectSidbasescope
expressionattrsr   zncacn_ip_tcp:{}[sign]z{}-{}zFailed to add computer '%s': z!Computer '%s' added successfully
)r'   r   rG   get_loadparmget_credentialsr   r   newcomputerresubcountrI   binary_encodesearch	domain_dnSCOPE_SUBTREEr	   r   dom_sidr   host_dns_name
descriptor	owner_sidget_domain_sid	group_sidrf   
get_logger	Exceptionoutfwrite)selfrz   r}   r|   r~   ro   r   r   r   rU   ry   ra   r   credsrP   hostnamefiltersrecsgroupownerrR   rS   r^   s                          r&   runcmd_computer_add.run   s;    " O&.*,' *J
++"#:#A#A*#MNN * ##%((,.	0an.>&+4El*5.=.=:U	     66&"l;>>#&&&'B\'QRRIPP%%h/1 ||*++&+[9	 $ ; Q 01!4"8#3#3T!W[5I!5LM$..+2253F3F3HI #+"5"5"7,1),4,<,<NN5#7#7#95A-)  X#U%8%8%:#__%68 			<|KL	  	0>+ ,-.0 0	0s    F!H   
H>*H99H> )	NNNNNNFNN__name__
__module____qualname____firstlineno____doc__synopsisr   strtakes_options
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsr   __static_attributes__r   r,   r&   rh   rh      s    > 0H 	tW#JS	2~L 		
 	%=CH"K"	$ 	~% 	  	)1&		 %M2 !!J ))..-- LPGL>BBMr,   rh   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_computer_deletei7  af  Delete a computer.

This command deletes a computer account from the Active Directory domain. The
computername specified on the command is the sAMAccountName without the
trailing $ (dollar sign).

Once the account is deleted, all permissions and memberships associated with
that account are deleted. If a new computer account is added with the same name
as a previously deleted account name, the new computer does not have the
previous permissions. The new account computer will be assigned a new security
identifier (SID) and permissions and memberships will have to be added.

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 computer delete Computer1 -H ldap://samba.samdom.example.com \
    -Uadministrator%passw1rd

Example1 shows how to delete a computer in the domain against a remote LDAP
server. The -H parameter is used to specify the remote target server. The
--computername= and --password= options are used to pass the computername and
password of a computer that exists on the remote server and is authorized to
issue the command on that server.

Example2:
sudo samba-tool computer delete Computer2

Example2 shows how to delete a computer in the domain against the local server.
sudo is used so a computer may run the command as root.

rj   rk   rl   rm   rn   ro   rp   rz   r{   Nc                 J   UR                  5       nUR                  USS9n[        U[        5       XvS9nUn	UR	                  S5      (       d  SU-  n	S[
        R                  " U	5      [        R                  4-  n
 UR                  UR                  5       [
        R                  U
SS/S	9nUS
   R                  n[        US
   S   S
   5      nSUS
   ;   a  [        US
   S   S
   5      nOS n U[        R"                  -  nU(       d  [!        SU-  5      e UR%                  U5        U(       a  ['        XR)                  5       USS9  U R,                  R/                  SU-  5        g ! [         a    [!        SU-  5      ef = f! [*         a  n[!        SU	-  U5      eS nAff = f)NTfallback_machiner   r0   %s$)(&(sAMAccountName=%s)(sAMAccountType=%u))userAccountControldNSHostNamer   r   Unable to find computer "%s"zNFailed to remove computer "%s": Computer is not a workstation - removal denied)ignore_no_namezFailed to remove computer "%s"zDeleted computer %s
)r   r   r   r   endswithrI   r   r   ATYPE_WORKSTATION_TRUSTr   r   r   dnintr   
IndexErrorr   UF_WORKSTATION_TRUST_ACCOUNTdeleter   r   r   r   r   )r   rz   r}   r|   r~   ro   r   r   rP   samaccountnamefilterr]   computer_dncomputer_accomputer_dns_host_namecomputer_is_workstationr^   s                    r&   r   cmd_computer_delete.rung  s   ##%((d(C!.*:"'0 &$$S))"\1N=$$^4//11	N,,EOO$5%(%6%6*0&:M%J  LC a&))Kc!f%9:1=>KA&),SVM-B1-E)F&)-&
 $;;; 	 &  P!- . / /	2LL%%%??,.D#') 			/,>?%  	N=LMM	N  	2?- ./02 2	2s+   ;A0E( ,E( 1F (F
F"FF"r   NNNNr   r   r,   r&   r   r   7  si     B 0H 	tW#JS	2M
 !!J))..-- :> $,@r,   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_computer_editi  a  Modify Computer AD object.

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

The computername specified on the command is the sAMaccountName with or
without the trailing $ (dollar sign).

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 computer edit Computer1 -H ldap://samba.samdom.example.com \
    -U administrator --password=passw1rd

Example1 shows how to edit a computers attributes in the domain against a
remote LDAP server.

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

Example2:
samba-tool computer edit Computer2

Example2 shows how to edit a computers attributes in the domain against a
local LDAP server.

Example3:
samba-tool computer edit Computer3 --editor=nano

Example3 shows how to edit a computers attributes in the domain against a
local LDAP server using the 'nano' editor.
rj   rk   rl   rm   rn   ro   rp   z--editorzQEditor to use instead of the system default, or 'vi' if no system default is set.ru   rz   r{   Nc                 t   UR                  5       nUR                  USS9n[        U[        5       XS9n	Un
UR	                  S5      (       d  SU-  n
S[
        R                  [        R                  " U
5      4-  nU	R                  5       n U	R                  UU[        R                  S9nUS   R                  n[        U5      S
:w  a  [        SU[        U5      4-  5      eUS   n[         R"                  " X5      nUc$  [$        R&                  R)                  S5      nUc  Sn[*        R,                  " SS9 nUR/                  [1        U5      5        UR3                  5          [5        UUR6                  /5        [;        UR6                  5       nUR=                  5       nS S S 5        S S S 5        U	R?                  W5      n[A        U5      S
   nU	RC                  UU5      n[        U5      S:X  a  U RD                  R/                  S5        g  U	RG                  U5        U RD                  R/                  SU-  5        g ! [         a    [        S	U-  5      ef = f! [8         a  n[9        SU5      eS nAff = f! , (       d  f       N= f! , (       d  f       N= f! [H         a  n[        SU-  U5      eS nAff = f)NTr   r   r0   r   )(&(sAMAccountType=%d)(sAMAccountName=%s))r   r   r   r   r   r   z'Invalid number of results: for "%s": %dEDITORviz.tmp)suffixzERROR: zNothing to do
z Failed to modify computer '%s': z$Modified computer '%s' successfully
)%r   r   r   r   r   r   r   rI   r   r   r   r   r   r   r   rH   r   get_ldif_for_editorosenvirongettempfileNamedTemporaryFiler   r   flushr   rQ   r   openread
parse_ldifnextmsg_diffr   modifyr   )r   rz   r}   r|   r~   ro   editorr   r   rP   r   r   domaindnr]   r   msgresult_ldift_filer^   edited_fileedited_messagemsgs_edited
msg_editedres_msg_diffs                           r&   r   cmd_computer_edit.run  s   ##%((d(C!.*:"'0 &$$S))"\1N=//$$^466 ??$	P,,H*0%(%6%6  8C a&))K s8q=H!-C9 : ; ; !f00<>ZZ^^H-F~((76LL;/0LLN7FFKK01 fkk"k!,!1!1!3 # 8 &&~6+&q)
~~c:6|!IIOO-.	0LL&
 			?,NOO  	P=NOO	P( & 7(A667"" 87(  	0A+ ,-.0 0	0sl   .H= 7+J#I:JI7 JJ =I
I4#I//I44J7
J	J
J
J7#J22J7r   NNNNNr   r   r,   r&   r   r     s~    "F 0H 	tW#JS	2z !7=@	BM !!J))..-- LP=Pr,   r   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_computer_listi	  zList all computers.z%prog [options]rk   rl   rm   rn   ro   rp   z-bz	--base-dnzSpecify base DN to useru   z	--full-dnfull_dnFrv   z)Display DN instead of the sAMAccountName.)rt   defaultrw   rq   r{   Nc           	         UR                  5       nUR                  USS9n[        U[        5       XS9n	S[        R
                  -  n
U	R                  5       nU(       a  U	R                  U5      nU	R                  U[        R                  U
S/S9n[        U5      S:X  a  g U He  nU(       a/  U R                  R                  SUR                  S	5      -  5        M9  U R                  R                  SUR                  SSS
9-  5        Mg     g )NTr   r   z(sAMAccountType=%u)r   )r   r   r   r   z%s
r   )idx)r   r   r   r   r   r   r   normalize_dn_in_domainr   rI   r   rH   r   r   r   )r   r|   r}   r~   ro   base_dnr   r   r   rP   r   	search_dnr]   r   s                 r&   r   cmd_computer_list.run   s     ##%((d(C!.*:"'0 '$*F*FGOO%	44W=Ill9!$!2!2&,"2!3  5 HMC		 67IIOOFSWW-=1W%EEF r,   r   )NNNNNF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r&   r   r   	  s     H 	tW#JS	2t[,	 	{"?	A
M ))..-- Gr,   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_computer_showiB  a  Display a computer AD object.

This command displays a computer account and it's attributes in the Active
Directory domain.
The computername specified on the command is the sAMAccountName.

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 computer show Computer1 -H ldap://samba.samdom.example.com \
    -U administrator

Example1 shows how display a computers attributes in the domain against a
remote LDAP server.

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

Example2:
samba-tool computer show Computer2

Example2 shows how to display a computers attributes in the domain against a
local LDAP server.

Example3:
samba-tool computer show Computer2 --attributes=objectSid,operatingSystem

Example3 shows how to display a computers objectSid and operatingSystem
attribute.
rj   rk   rl   rm   rn   ro   rp   z--attributesz:Comma separated list of attributes, which will be printed.computer_attrs)rq   rr   rt   rz   r{   Nc                 V   UR                  5       nUR                  USS9n[        U[        5       XS9n	S n
U(       a  UR	                  S5      n
UnUR                  S5      (       d  SU-  nS[        R                  [        R                  " U5      4-  nU	R                  5       n U	R                  X[        R                  U
S9nUS	   R                  nU H5  n[         R"                  " U	U5      nU R$                  R'                  U5        M7     g ! [         a    [        S
U-  5      ef = f)NTr   r   ,r0   r   r   )r   r   r   r   r   r   )r   r   r   r   splitr   r   r   rI   r   r   r   r   r   r   r   r   r   r   r   )r   rz   r}   r|   r~   ro   r  r   r   rP   r   r   r   r   r]   r   r   computer_ldifs                     r&   r   cmd_computer_show.runv  s3    ##%((d(C!.*:"'0 "((-E%$$S))"\1N=//$$^466 ??$	/,,H%(%6%6e  ECa&))K
 C"66ucBMIIOOM* 	  	/=- . / /	/s   %.D D(r   r   r   r   r,   r&   r  r  B  s     B 0H 	tW#JS	2~..	0M !!J))..-- LP#' +r,   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_computer_movei  z4Move a computer to an organizational unit/container.z*%prog <computername> <new_ou_dn> [options]rk   rl   rm   rn   ro   rp   rz   	new_ou_dnr{   Nc                    UR                  5       nUR                  USS9n[        U[        5       XS9n	[        R
                  " XR                  5       5      n
UnUR                  S5      (       d  SU-  nS[        R                  " U5      [        R                  4-  n U	R                  U
U[        R                  S9nUS   R                  n[        R
                  " X5      nUR!                  U
5      (       d  UR#                  U
5        [        R
                  " U	[%        U5      5      nUR'                  [)        U5      S
-
  5        UR#                  U5         U	R+                  UU5        U R.                  R1                  SU< SU< S35        g ! [         a    [        S	U-  5      ef = f! [,         a  n[        SU-  U5      eS nAff = f)NTr   r   r0   r   r   r   r   r   r   zFailed to move computer "%s"zMoved computer "z" to "z"
)r   r   r   r   rI   rJ   r   r   r   r   r   r   r   r   r   r   is_child_ofadd_baser   remove_base_componentsrH   renamer   r   r   )r   rz   r  r}   r|   r~   ro   r   r   rP   r   r   r   r]   r   full_new_ou_dnnew_computer_dnr^   s                     r&   r   cmd_computer_move.run  s   ##%((d(C!.*:"'0FF5//"34	%$$S))"\1N=$$^4//11	P,,I*0%(%6%6  8C a&))K 1)))44##I.&&K(89..s;/?/BC  0	QLLo6 			%y2 	3  	P=NOO	P  	Q=LaPP	Qs$   .F F, F),
G
6GG
r   r   r   r   r,   r&   r  r    sg    >;H 	tW#JS	2M
 !+.J))..-- EI $"3r,   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	'   S
rg)cmd_computeri  zComputer management.addcreater   editlistshowmover   N)r   r   r   r   r   subcommandsrh   r   r   r   r  r  r   r   r,   r&   r  r    se    K)+K,.K/1K+-K+-K+-K+-Kr,   r  )N)7samba.getoptgetoptr   rI   r   sambar   r   r   r   samba.dcerpcr   r   r   samba.dnsserverr   r   	samba.ndrr	   r
   r   samba.remove_dcr   
samba.authr   samba.samdbr   samba.commonr   
subprocessr   r   r   r   r   r   r   r   r   samba.netcmdr   r   r   r   r'   r+   r.   rf   rh   r   r   r   r  r  r  r   r,   r&   <module>r*     s   .  
   	 	   2 2 / 5 5 1 %  " 5   F
G
[
|EMw EMP\@' \@~qP qPf6G 6GrT+ T+n33 33l
.< 
.r,   