a
    Wad                     @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZmZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ ddl m!Z! d dlm"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+ d%ddZ,dd Z-dd Z.dd Z/G dd de(Z0G dd de(Z1G dd de(Z2G dd de(Z3G dd  d e(Z4G d!d" d"e(Z5G d#d$ d$e*Z6dS )&    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              	   C   sL   |st jt jg}|D ]2}zt ||  W  dS  t jyD   Y qY q0 qdS )z Check ip string is valid addressTF)socketAF_INETAF_INET6Z	inet_ptonerror)	ip_stringaddress_familiesZaddress_family r"   7/usr/lib/python3/dist-packages/samba/netcmd/computer.py_is_valid_ip8   s    r$   c                 C   s   t | tjgdS )z%Check ip string is valid ipv4 addressr!   )r$   r   r   r    r"   r"   r#   _is_valid_ipv4G   s    r'   c                 C   s   t | tjgdS )z%Check ip string is valid ipv6 addressr%   )r$   r   r   r&   r"   r"   r#   _is_valid_ipv6L   s    r(   c                 C   s  | d}tj}tjtjB }|  }	d}
t| }z&||d||	|dt	j
|dd
\}}W n8 ty } z |jd tjkr|d}
W Y d}~n
d}~0 0 |
r,|jD ]}|jD ]}|jt	jks|jt	jkrt }||_z||d||	|d| W q ty& } z |jd tjkr W Y d}~qd}~0 0 qq|D ]}t|r\|d||	|f  t|}n6t|r|d||	|f  t|}ntd|t }||_||d||	||d q0t|dkrt| d	|    }| j!d
||	f |d\}}|j"||dt#j$t#j%B  gd dS )z3Add DNS A or AAAA records while creating computer. $Tr   NFz,Adding DNS AAAA record %s.%s for IPv6 IP: %sz)Adding DNS A record %s.%s for IPv4 IP: %szInvalid IP: {}zDC=DomainDnsZones,%sz%s.%s)Zdns_partitionzsd_flags:1:%d)Zcontrols)&rstripr   ZDNS_CLIENT_VERSION_LONGHORNZDNS_RPC_VIEW_AUTHORITY_DATAZDNS_RPC_VIEW_NO_CHILDRENZdomain_dns_namer   ZSDUtilsZDnssrvEnumRecords2r   ZDNS_TYPE_ALLr   argsr   Z"WERR_DNS_ERROR_NAME_DOES_NOT_EXISTrecZrecordsZwTypeZ
DNS_TYPE_AZDNS_TYPE_AAAAZDNS_RPC_RECORD_BUFZDnssrvUpdateRecord2r(   infor   r'   r   
ValueErrorformatlenldbDnZget_default_basednZ
dns_lookupZmodify_sd_on_dnr   ZSECINFO_OWNERZSECINFO_GROUP)samdbnamedns_connchange_owner_sdZserverip_address_listZloggerZclient_versionZselect_flagsZzoneZ
name_foundZ	sd_helperZbuflenreser,   recordZdel_rec_buf
ip_addressZadd_rec_bufZdomaindns_zone_dnZdns_a_dnZldap_recordr"   r"   r#   add_dns_recordsQ   s    



	







r<   c                   @   s   e Zd ZdZdZedddeddded	d
ededdededdddedddddedddddgZdgZe	j
e	je	jdZd ddZdS )!cmd_computer_createa?  Create a new computer.

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

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

Example1 shows how to create a new computer in 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 create Computer2

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

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

Example3 shows how to create 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>'rE   rF   z--descriptionzComputers's descriptionz--prepare-oldjoinz5Prepare enabled machine account for oldjoin mechanism
store_true)rE   actionz--ip-addressr7   ziIPv4 address for the computer's A record, or IPv6 address for AAAA record, can be provided multiple timesappend)rH   rE   rK   z--service-principal-nameservice_principal_name_listzAComputer's Service Principal Name, can be provided multiple timescomputername	sambaoptscredoptsversionoptsNFc              
   C   s  |	d u rg }	|
d u rg }
|	D ]}t |std|q| }||}zt|t ||d}|j|||||	|
d |	rNt	dd|}|
drtd| dt|}|j| tj|d	d
gd}|d d	 d }ttj|d d
 d }td| ||}t }||_td| ||_t||||| |	|   W n4 ty } ztd| |W Y d }~n
d }~0 0 | jd|  d S )NzInvalid IP address {}ZurlZsession_infor   lp)
computeroudescriptionprepare_oldjoinr7   rM   z\$$ r)   zIllegal computername "%s"z-(&(sAMAccountName={}$)(objectclass=computer))ZprimaryGroupIDZ	objectSidbasescope
expressionattrsr   zncacn_ip_tcp:{}[sign]z{}-{}z Failed to create computer '%s': z#Computer '%s' created successfully
)r$   r   r/   get_loadparmget_credentialsr   r   Znewcomputerresubcountr1   binary_encodesearch	domain_dnSCOPE_SUBTREEr   r   Zdom_sidr   Zhost_dns_nameZ
descriptorZ	owner_sidZget_domain_sidZ	group_sidr<   
get_logger	Exceptionoutfwrite)selfrN   rQ   rP   rR   rC   rU   rV   rW   r7   rM   r;   rT   credsr3   ZhostnamefiltersZrecsgroupownerr5   r6   r9   r"   r"   r#   run   sn    

zcmd_computer_create.run)	NNNNNNFNN__name__
__module____qualname____doc__synopsisr   strtakes_optionsZ
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsrp   r"   r"   r"   r#   r=      sD      r=   c                   @   sL   e Zd ZdZdZedddedddgZd	gZe	j
e	je	jd
ZdddZdS )cmd_computer_deleteaf  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.

r>   r?   r@   rA   rB   rC   rD   rN   rO   Nc              
   C   sb  |  }|j|dd}t|t ||d}|}	|ds>d| }	dt|	tjf }
zd|j	|
 tj|
ddgd	}|d
 j}t|d
 d d
 }d|d
 v rt|d
 d d
 }nd }W n ty   td| Y n0 |tj@ }|std| z(|| |rt||  |dd W n4 tyL } ztd|	 |W Y d }~n
d }~0 0 | jd|  d S )NTZfallback_machinerS   r)   %s$)(&(sAMAccountName=%s)(sAMAccountType=%u))ZuserAccountControlZdNSHostNamerY   r   Unable to find computer "%s"zNFailed to remove computer "%s": Computer is not a workstation - removal denied)Zignore_no_namezFailed to remove computer "%s"zDeleted computer %s
)r^   r_   r   r   endswithr1   rc   r   ATYPE_WORKSTATION_TRUSTrd   re   rf   dnintrw   
IndexErrorr   ZUF_WORKSTATION_TRUST_ACCOUNTdeleter   rg   rh   ri   rj   )rk   rN   rQ   rP   rR   rC   rT   rl   r3   samaccountnamefilterr8   computer_dnZcomputer_acZcomputer_dns_host_nameZcomputer_is_workstationr9   r"   r"   r#   rp   h  s\    





zcmd_computer_delete.run)NNNNrq   r"   r"   r"   r#   r~   8  s   !  r~   c                   @   sX   e Zd ZdZdZedddeddded	d
edgZdgZe	j
e	je	jdZdddZdS )cmd_computer_editay  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.
    r>   r?   r@   rA   rB   rC   rD   z--editorzQEditor to use instead of the system default, or 'vi' if no system default is set.rI   rN   rO   Nc                 C   sD  |  }|j|dd}t|t ||d}	|}
|ds>d| }
dtjt|
f }|		 }z |	j
||tjd}|d j}W n ty   td	| Y n0 t|d
krtd|t|f |d }t|	|}|d u rtjd}|d u rd}tjdd}|t| |  zt||jg W n0 tyT } ztd|W Y d }~n
d }~0 0 t|j}| }W d    n1 s0    Y  W d    n1 s0    Y  |	|}t |d
 }|	!||}t|dkr| j"d d S z|	#| W n6 t$y. } ztd||f W Y d }~n
d }~0 0 | j"d|  d S )NTr   rS   r)   r   )(&(sAMAccountType=%d)(sAMAccountName=%s))rZ   r\   r[   r   r   r   z'Invalid number of results: for "%s": %dZEDITORZ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   r1   rc   re   rd   rf   r   r   r   r0   r   get_ldif_for_editorosenvirongettempfileZNamedTemporaryFilerj   r   flushr   r4   r   openreadZ
parse_ldifnextZmsg_diffri   Zmodifyrh   )rk   rN   rQ   rP   rR   rC   ZeditorrT   rl   r3   r   r   domaindnr8   r   msgZresult_ldifZt_filer9   Zedited_fileZedited_messageZmsgs_editedZ
msg_editedZres_msg_diffr"   r"   r#   rp     sl    

 H
zcmd_computer_edit.run)NNNNNrq   r"   r"   r"   r#   r     s"   #  r   c                	   @   sd   e Zd ZdZdZedddeddded	d
dededdddddgZej	ej
ejdZdddZdS )cmd_computer_listzList all computers.z%prog [options]r?   r@   rA   rB   rC   rD   z-bz	--base-dnzSpecify base DN to userI   z	--full-dnfull_dnFrJ   z)Display DN instead of the sAMAccountName.)rH   defaultrK   rE   rO   Nc                 C   s   |  }|j|dd}t|t ||d}	dtj }
|	 }|rH|	|}|	j|t	j
|
dgd}t|dkrnd S |D ]:}|r| jd|d	  qr| jd|jddd
  qrd S )NTr   rS   z(sAMAccountType=%u)r   )r[   r\   r]   r   z%s
r   )idx)r^   r_   r   r   r   r   re   Znormalize_dn_in_domainrd   r1   rf   r0   ri   rj   r   )rk   rP   rQ   rR   rC   Zbase_dnr   rT   rl   r3   r   Z	search_dnr8   r   r"   r"   r#   rp   !  s*    

zcmd_computer_list.run)NNNNNF)rr   rs   rt   ru   rv   r   rw   rx   ry   rz   r{   r|   r}   rp   r"   r"   r"   r#   r   
  s4         r   c                   @   sZ   e Zd ZdZdZedddeddded	d
eddgZdgZe	j
e	je	jdZdddZdS )cmd_computer_showa  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.
r>   r?   r@   rA   rB   rC   rD   z--attributesz:Comma separated list of attributes, which will be printed.computer_attrs)rE   rF   rH   rN   rO   Nc                 C   s   |  }|j|dd}t|t ||d}	d }
|r:|d}
|}|dsPd| }dtjt	|f }|	
 }z"|	j||tj|
d}|d	 j}W n ty   td
| Y n0 |D ]}t|	|}| j| qd S )NTr   rS   ,r)   r   r   )rZ   r\   r[   r]   r   r   )r^   r_   r   r   splitr   r   r   r1   rc   re   rd   rf   r   r   r   r   r   ri   rj   )rk   rN   rQ   rP   rR   rC   r   rT   rl   r3   r]   r   r   r   r8   r   r   Zcomputer_ldifr"   r"   r#   rp   w  s:    

zcmd_computer_show.run)NNNNNrq   r"   r"   r"   r#   r   C  s$   !	  r   c                   @   sN   e Zd ZdZdZedddedddgZd	d
gZe	j
e	je	jdZdddZdS )cmd_computer_movez4Move a computer to an organizational unit/container.z(%prog computername <new_ou_dn> [options]r?   r@   rA   rB   rC   rD   rN   	new_ou_dnrO   Nc              
   C   sL  |  }|j|dd}t|t ||d}	t|	|	 }
|}|dsNd| }dt|t	j
f }z |	j|
|tjd}|d j}W n ty   td	| Y n0 t|	|}||
s||
 t|	t|}|t|d
  || z|	|| W n4 ty2 } ztd| |W Y d }~n
d }~0 0 | jd||f  d S )NTr   rS   r)   r   r   r   r   r   r   zFailed to move computer "%s"zMoved computer "%s" to "%s"
)r^   r_   r   r   r1   r2   re   r   rc   r   r   rd   rf   r   r   r   Zis_child_ofZadd_baserw   Zremove_base_componentsr0   renamerh   ri   rj   )rk   rN   r   rQ   rP   rR   rC   rT   rl   r3   re   r   r   r8   r   Zfull_new_ou_dnZnew_computer_dnr9   r"   r"   r#   rp     sF    



$zcmd_computer_move.run)NNNNrq   r"   r"   r"   r#   r     s     r   c                   @   sP   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< dS )	cmd_computerzComputer management.Zcreater   ZeditlistZshowZmoveN)rr   rs   rt   ru   Zsubcommandsr=   r~   r   r   r   r   r"   r"   r"   r#   r     s   




r   )N)7Zsamba.getoptZgetoptry   r1   r   Zsambar`   r   r   r   Zsamba.dcerpcr   r   r   Zsamba.dnsserverr   r   Z	samba.ndrr   r	   r
   Zsamba.remove_dcr   Z
samba.authr   Zsamba.samdbr   Zsamba.compatr   
subprocessr   r   rX   r   r   r   r   r   r   Zsamba.netcmdr   r   r   r   r$   r'   r(   r<   r=   r~   r   r   r   r   r   r"   r"   r"   r#   <module>   s<   
_ 	_s9W6