a
    I_;                  
   @   s   d dl Z d dl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 d dlmZ d dlmZmZmZmZmZ d	Zd
ZddddddddZededed
ed
ediZededededediZg ZG dd deZG dd deZdS )    N)	b64encode)sd_utils)
ndr_unpackndr_pack)security)SECINFO_DACL)'get_managed_service_accounts_descriptor)DS_DOMAIN_FUNCTION_2008DS_DOMAIN_FUNCTION_2008_R2DS_DOMAIN_FUNCTION_2012DS_DOMAIN_FUNCTION_2012_R2DS_DOMAIN_FUNCTION_2016K   Q   z$5e1574f6-55df-493e-a671-aaeffca6a100z$d262aae8-41f7-48ed-9f35-56bbb677573dz$82112ba0-7e4c-4a44-89d9-d46c9612bf91z$c3c927a6-cc1d-47c0-966b-be8f9b63d991z$54afcfb9-637a-4251-9f47-4d50e7021211z$f4728883-84dd-483c-9897-274f2ebcf11ez$ff4f9d27-7157-4cb0-80a9-5d6f2b14c8ff)r   L   M   N   O   P   r   J   r   X         	   
      c                   @   s   e Zd ZdS )DomainUpdateExceptionN)__name__
__module____qualname__ r    r    5/usr/lib/python3/dist-packages/samba/domain_update.pyr   J   s   r   c                   @   s   e Zd ZdZd&ddZd'ddZd	d
 Zd(ddZdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZdS ))DomainUpdatez2Check and update a SAM database for domain updatesFTc                 C   s   || _ || _|| _d| _| j  | _| j  | _| j  | _t	
|| _	t| | _| j  | _| jdsxtd| j  | _| jdstddS )z
        :param samdb: LDB database
        :param fix: Apply the update if the container is missing
        :param add_update_container: Add the container at the end of the change
        :raise DomainUpdateException:
        Fz(CN=Operations,CN=DomainUpdates,CN=Systemz+Failed to add domain update container childz3CN=ActiveDirectoryUpdate,CN=DomainUpdates,CN=Systemz#Failed to add revision object childN)samdbfixadd_update_containerZcheck_update_appliedZget_config_basednZ	config_dn	domain_dnZget_schema_basednZ	schema_dnr   ZSDUtilsr   Zdom_sidZget_domain_sid
domain_sidZget_root_basedndomainupdate_containerZ	add_childr   revision_object)selfr#   r$   r%   r    r    r!   __init__Q   s    zDomainUpdate.__init__Nc           	      C   s   | j j| jdgtjd}t| }|r6t| }|d7 }nt}| || t| }t	|d d d }|r||k r| j
std||f | j dt| j|f  dS )a  
        Apply all updates for a given old and new functional level
        :param functional_level: constant
        :param old_functional_level: constant
        :param update_revision: modify the stored version
        :raise DomainUpdateException:
        Zrevision)baseattrsscope   r   zERevision is not high enough. Fix is set to False.
Expected: %dGot: %dz9dn: %s
changetype: modify
replace: revision
revision: %d
N)r#   searchr)   ldb
SCOPE_BASEfunctional_level_to_max_update
MIN_UPDATEcheck_updates_rangefunctional_level_to_versionintr$   r   modify_ldifstr)	r*   Zfunctional_levelZold_functional_levelZupdate_revisionresZexpected_updateZ
min_updateZexpected_versionZfound_versionr    r    r!   check_updates_functional_leveln   s*    


z+DomainUpdate.check_updates_functional_levelc                 C   s8   |D ].}|t k s|tkr tdt| d| | qdS )z
        Apply a list of updates which must be within the valid range of updates
        :param iterator: Iterable specifying integer update numbers to apply
        :raise DomainUpdateException:
        Update number invalid.operation_%dN)r4   
MAX_UPDATEr   getattr)r*   iteratoropr    r    r!   check_updates_iterator   s    z#DomainUpdate.check_updates_iteratorr   c                 C   sT   |}|t k s||ks|tkr$td||krP|tvrFt| d| | |d7 }q$dS )z
        Apply a range of updates which must be within the valid range of updates
        :param start: integer update to begin
        :param end: integer update to end (inclusive)
        :raise DomainUpdateException:
        r<   r=   r/   N)r4   r>   r   missing_updatesr?   )r*   startendrA   r    r    r!   r5      s    z DomainUpdate.check_updates_rangec                 C   sB   z| j j| jdt|  d}W n tjy4   Y dS 0 t|dkS )zd
        :param op: Integer update number
        :return: True if update exists else False
        z(CN=%s))r,   
expressionFr/   )r#   r0   r(   
update_mapr1   ZLdbErrorlen)r*   rA   r:   r    r    r!   update_exists   s    


zDomainUpdate.update_existsc                 C   s"   | j dt| t| jf  dS )zo
        Add the corresponding container object for the given update
        :param op: Integer update
        z$dn: CN=%s,%s
objectClass: container
N)r#   add_ldifrG   r9   r(   r*   rA   r    r    r!   
update_add   s    zDomainUpdate.update_addc                 C   s`   | d}|dkr0|d| | ||d  }n|| }||v rDdS | jj||dt gd dS )a  
        Add an ACE to a DACL, checking if it already exists with a simple string search.

        :param dn: DN to modify
        :param existing_sddl: existing sddl as string
        :param ace: string ace to insert
        :return: True if modified else False
        S:NFsd_flags:1:%dcontrolsT)rfindr   Zmodify_sd_on_dnr   )r*   dnexisting_sddlaceindexnew_sddlr    r    r!   insert_ace_into_dacl   s    	

z!DomainUpdate.insert_ace_into_daclc           	      C   s   | j j||gdgd}t|dks&J |d | d }|d}|dkrf|d| | ||d  }n|| }||v rzdS t }||_t|tj|||< | j j	|d	gd
 dS )aC  
        Insert an ACE into a string attribute like defaultSecurityDescriptor.
        This also checks if it already exists using a simple string search.

        :param dn: DN to modify
        :param ace: string ace to insert
        :param attr: attribute to modify
        :return: True if modified else False
        search_options:1:2)r,   r-   rQ   r/   r   rM   rN   NFrelax:0rP   T)
r#   r0   rH   rR   r1   ZMessagerS   ZMessageElementZFLAG_MOD_REPLACEZmodify)	r*   rS   rU   attrmsgrT   rV   rW   mr    r    r!   insert_ace_into_string   s&    


z#DomainUpdate.insert_ace_into_stringc                 C   s   | j std| dS )z
        Raises an exception if not set to fix.
        :param op: Integer operation
        :raise DomainUpdateException:
        z3Missing operation %d. Fix is currently set to FalseN)r$   r   rK   r    r    r!   raise_if_not_fix  s    zDomainUpdate.raise_if_not_fixc                 C   sF   |  |rd S | | | jjd| j ddgd | jrB| | d S )NzVdn: CN=TPM Devices,%s
objectClass: top
objectClass: msTPM-InformationObjectsContainer
rZ   provision:0rP   )rI   r_   r#   rJ   r&   r%   rL   rK   r    r    r!   operation_78  s    

zDomainUpdate.operation_78c                 C   s   |  |rd S | | d}| jjddgdgd}|D ]4}ttj|d d }|| j}| 	|j
|| q6| jjddgdgd}|D ]4}ttj|d d }|| j}| 	|j
|| q| jr| | d S )NzY(OA;CIIO;WP;ea1b7b93-5e48-46d5-bc6c-4df4fda78a35;bf967a86-0de6-11d0-a285-00aa003049e2;PS)(objectClass=samDomain)nTSecurityDescriptorrY   rF   r-   rQ   r   (objectClass=domainDNS)rI   r_   r#   r0   r   r   
descriptoras_sddlr'   rX   rS   r%   rL   r*   rA   rU   r:   r\   Zexisting_sdrT   r    r    r!   operation_79  s4    



zDomainUpdate.operation_79c                 C   s   |  |rd S | | dt| j }| jj| jtjdgddt	 gd}|d }t
tj|d d }|| j}| |j|| | jr| | d S )Nz5(OA;;CR;3e0f7e18-2c7a-4c10-ba82-4d926db99a3e;;%s-522)rc   rY   rO   )r,   r.   r-   rQ   r   )rI   r_   r9   r'   r#   r0   r&   r1   r2   r   r   r   rg   rh   rX   rS   r%   rL   ri   r    r    r!   operation_809  s&    



zDomainUpdate.operation_80c                 C   s   |  |rd S | | d}| jjddgdgd}|D ]4}ttj|d d }|| j}| 	|j
|| q6| jjddgdgd}|D ]4}ttj|d d }|| j}| 	|j
|| q| jr| | d S )Nz7(OA;CIOI;RPWP;3f78c3e5-f79a-46bd-a0b8-9d18116ddc79;;PS)rb   rc   rY   rd   r   re   rf   ri   r    r    r!   operation_81R  s4    



zDomainUpdate.operation_81c                 C   sn   |  |rd S | | t| j}t|d}dt| j }| jj	d||f ddgd | j
rj| | d S )Nutf8CN=Managed Service Accounts,%szdn: %s
changetype: add
objectClass: container
description: Default container for managed service accounts
showInAdvancedViewOnly: FALSE
nTSecurityDescriptor:: %srZ   r`   rP   )rI   r_   r   r'   r   decoder9   r&   r#   r8   r%   rL   )r*   rA   rg   Zmanagedservice_descrmanaged_service_dnr    r    r!   operation_75v  s    


zDomainUpdate.operation_75c                 C   s\   |  |rd S | | dt| j }| jjdt| j|f ddgd | jrX| | d S )Nrn   zudn: %s
changetype: modify
add: otherWellKnownObjects
otherWellKnownObjects: B:32:1EB93889E40C45DF9F0C64D23BBB6237:%s
rZ   r`   rP   )rI   r_   r9   r&   r#   r8   r%   rL   )r*   rA   rp   r    r    r!   operation_76  s    

zDomainUpdate.operation_76c                 C   sJ   |  |rd S | | | jjdt| j ddgd | jrF| | d S )NzFdn: CN=PSPs,CN=System,%s
objectClass: top
objectClass: msImaging-PSPs
rZ   r`   rP   )rI   r_   r#   rJ   r9   r&   r%   rL   rK   r    r    r!   operation_77  s    

zDomainUpdate.operation_77)FT)NF)r   r   )r   r   r   __doc__r+   r;   rB   r5   rI   rL   rX   r^   r_   ra   rj   rk   rl   rq   rr   rs   r    r    r    r!   r"   N   s*     
  
$
	#
$r"   )r1   Zsambabase64r   r   Z	samba.ndrr   r   Zsamba.dcerpcr   Zsamba.dcerpc.securityr   Zsamba.descriptorr   Z
samba.dsdbr	   r
   r   r   r   r4   r>   rG   r3   r6   rC   	Exceptionr   objectr"   r    r    r    r!   <module>   sB   		