HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/samba/kcc/__pycache__/kcc_utils.cpython-310.pyc
o

�/a�W�@sdddlZddlZddlZddlmZddlmZmZmZddl	m
Z
ddlmZm
Z
ddlmZGdd�de�ZGd	d
�d
e�Zedd�ej��D��ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd �d e�Z!Gd!d"�d"e�Z"d#d$�Z#d%d&�Z$d'd(�Z%dS))�N)�dsdb)�drsblobs�drsuapi�misc)�dsdb_Dn)�
ndr_unpack�ndr_pack)�Counterc@seZdZdS)�KCCErrorN)�__name__�
__module__�__qualname__�rr�5/usr/lib/python3/dist-packages/samba/kcc/kcc_utils.pyr
%sr
c@s eZdZedd�\ZZZZZdS)�NCTyper�N)	rrr
�range�unknown�schema�domain�config�applicationrrrrr)srccs,�|]\}}|dd�dkr||fVqdS)N��__r)�.0�k�vrrr�	<genexpr>.s�*rc@�@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�
NamingContextz�Base class for a naming context.

    Holds the DN, GUID, SID (if available) and type of the DN.
    Subclasses may inherit from this and specialize
    cCs||_d|_d|_tj|_dS)zKInstantiate a NamingContext

        :param nc_dnstr: NC dn string
        N)�nc_dnstr�nc_guid�nc_sidrr�nc_type��selfr rrr�__init__8szNamingContext.__init__cCs^d|jjfd|jdt|j�}|jdur|d}n|d}|dt|j|jf}|S)�!Debug dump string output of class�%s:z
	nc_dnstr=%sz
	nc_guid=%sNz
	nc_sid=<absent>z
	nc_sid=<present>z
	nc_type=%s (%s))�	__class__rr �strr!r"�
nctype_lutr#�r%�textrrr�__str__Bs��

�zNamingContext.__str__c
Cs�ddg}z|j|jtj|d�}Wntjy+}z|j\}}td|j|f��d}~ww|d}d|vrBt�|�	d|dd��|_
d|vrM|dd|_|j
dusTJ�dS)N�
objectGUID�	objectSid��base�scope�attrsz)Unable to find naming context (%s) - (%s)r)�searchr �ldb�
SCOPE_BASE�LdbError�argsr
r�GUID�schema_format_valuer!r")r%�samdbr4�res�e�enum�estr�msgrrr�load_ncQs,�
�
���

�zNamingContext.load_nccCs|jtjksJ�|jtjkS)zReturn True if NC is config)r#rrr�r%rrr�	is_configeszNamingContext.is_configcCsr|jdur
|�|�|jt|���krtj|_dS|jt|���kr(tj	|_dS|j
dur3tj|_dStj|_dS)zoGiven an NC object, identify what type is is thru
           the samdb basedn strings and NC sid value
        N)
r!rBr r*�get_schema_basednrrr#�get_config_basednrr"rr)r%r<rrr�identify_by_basednjs


z NamingContext.identify_by_basedncCsT|dkr	tj|_n|dkrtj|_n	|dkr|�|�|jtjkr(|�|�dSdS)a>Given an NC which has been discovered thru the
        nTDSDSA database object, determine what type of NC
        it is (i.e. schema, config, domain, application) via
        the use of the schema attribute under which the NC
        was found.

        :param attr: attr of nTDSDSA object where NC DN appears
        �msDS-HasDomainNCs�hasPartialReplicaNCs�hasMasterNCsN)rrr#rGr�r%r<�attrrrr�identify_by_dsa_attr�s


�z"NamingContext.identify_by_dsa_attrN)
rrr
�__doc__r&r.rBrDrGrMrrrrr1s
rc@s�eZdZdZdd�Zdd�Zd'dd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zd(dd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd(d$d%�Zd&S))�	NCReplicaz�Naming context replica that is relative to a specific DSA.

    This is a more specific form of NamingContext class (inheriting from that
    class) and it identifies unique attributes of the DSA's replica for a NC.
    cCsP|j|_|j|_d|_d|_d|_d|_d|_g|_	g|_
d|_t�
||�dS)z�Instantiate a Naming Context Replica

        :param dsa_guid: GUID of DSA where replica appears
        :param nc_dnstr: NC dn string
        FrN)�	dsa_dnstr�
rep_dsa_dnstr�dsa_guid�rep_dsa_guid�rep_default�rep_partial�rep_ro�rep_instantiated_flags�rep_fsmo_role_owner�rep_repsFrom�
rep_repsTo�rep_present_criteria_onerr&)r%�dsar rrrr&�szNCReplica.__init__cCs�d|jjd|jd|jd|jd|jd|jd|��d|jd	�	d
d�|j
D��d	�	dd�|jD��}d
t�
|�|fS)r'r(�
	dsa_dnstr=%s�
	dsa_guid=%sz
	default=%s�
	ro=%sz
	partial=%sz
	present=%sz
	fsmo_role_owner=%s�cs��|]}d|VqdS��
%sNr�r�reprrrr���z$NCReplica.__str__.<locals>.<genexpr>csrarbrrdrrrr�rfz%s
%s)r)rrQrSrTrVrU�
is_presentrX�joinrYrZrr.r,rrrr.�s(
�����
���	�zNCReplica.__str__rcCs
||_dS)z*Set or clear NC replica instantiated flagsN)rW)r%�flagsrrr�set_instantiated_flags�s
z NCReplica.set_instantiated_flagscCs�|dkrd|_d|_n:|dkrd|_n2|dkr0d|_|jt|���kr/|jt|���kr/d|_n|dkr;d|_d|_n
|dkrEd|_d|_t�	|||�dS)	z�Given an NC which has been discovered thru the
        nTDSDSA database object, determine what type of NC
        replica it is (i.e. partial, read only, default)

        :param attr: attr of nTDSDSA object where NC DN appears
        rITrHrJ�msDS-hasFullReplicaNCs�msDS-hasMasterNCsFN)
rUr[rTr r*rErFrVrrMrKrrrrM�s$	
�zNCReplica.identify_by_dsa_attrcC�|jS)zMWhether this is a default domain for the dsa that this NC appears on
        )rTrCrrr�
is_default�zNCReplica.is_defaultcCrm)z&Return True if NC replica is read only)rVrCrrr�is_ro�zNCReplica.is_rocCrm)z$Return True if NC replica is partial)rUrCrrr�
is_partialrqzNCReplica.is_partialcCs|jr
|jtj@dkr
dSdS)a7Given an NC replica which has been discovered thru the
        nTDSDSA database object and populated with replica flags
        from the msDS-HasInstantiatedNCs; return whether the NC
        replica is present (true) or if the IT_NC_GOING flag is
        set then the NC replica is not present (false)
        rTF)r[rWr�INSTANCE_TYPE_NC_GOINGrCrrrrgszNCReplica.is_presentcC��z
|j|jtjdgd�}Wntjy(}z|j\}}td|j|f��d}~ww|d}d|vrh|dD]4}zttj	|�}Wnt
yZ}	ztd|tj
d�WYd}	~	q5d}	~	wwt|j|�}
|j�|
�q5dSdS)a*Given an NC replica which has been discovered thru the nTDSDSA
        database object, load the repsFrom attribute for the local replica.
        held by my dsa.  The repsFrom attribute is not replicated so this
        attribute is relative only to the local DSA that the samdb exists on
        �repsFromr1�!Unable to find NC for (%s) - (%s)Nrzbad repsFrom NDR: %r��file)r5r r6r7r8r9r
rr�repsFromToBlob�RuntimeError�print�sys�stderr�
RepsFromTorY�append)r%r<r=�e1r?r@rA�value�unpackedr>rerrr�
load_repsFrom%s6
�
�������zNCReplica.load_repsFromFc	
C��d}g}g}|jD]}|jr|�|�d}q	|��r |��d}|�t|j��q	|D]}|j�|�q+g}|r:|r<dSt�	�}t�
||j�|_t�
|tjd�|d<z|�|�WdStjyp}z	td|j|f��d}~ww)zCommit repsFrom to the databaseFTNruz&Could not set repsFrom for (%s) - (%s))rY�
to_be_deletedr�is_modified�set_unmodifiedr�ndr_blob�remover6�Message�Dnr �dn�MessageElement�FLAG_MOD_REPLACE�modifyr8r
)	r%r<�ror��newreps�delrepsru�mr@rrr�commit_repsFromB�:


����zNCReplica.commit_repsFromc	
Cs�z
|j|jtjdgd�}Wntjy(}z|j\}}td|j|f��d}~ww|d}d|vrP|dd}ttj	|�}|j
dkrItd|j
��|jj
|_dSg|_dS)aFGiven an NC replica which has been discovered thru the nTDSDSA
        database object, load the replUpToDateVector attribute for the
        local replica. held by my dsa. The replUpToDateVector
        attribute is not replicated so this attribute is relative only
        to the local DSA that the samdb exists on

        �replUpToDateVectorr1rvNrrz(Unexpected replUpToDateVector version %d)r5r r6r7r8r9r
rr�replUpToDateVectorBlob�version�AttributeError�ctr�cursors�rep_replUpToDateVector_cursors)	r%r<r=�e2r?r@rAr��blobrrr�load_replUpToDateVector�s.
�
����
�
z!NCReplica.load_replUpToDateVectorcC�d�dd�|jD��S)N�
css�|]
}|jrt|�VqdS�N)r�r*�r�xrrrr�s�z2NCReplica.dumpstr_to_be_deleted.<locals>.<genexpr>�rhrYrCrrr�dumpstr_to_be_deleted��zNCReplica.dumpstr_to_be_deletedcCr�)Nr�css �|]}|��rt|�VqdSr�)r�r*r�rrrr�s�z3NCReplica.dumpstr_to_be_modified.<locals>.<genexpr>r�rCrrr�dumpstr_to_be_modified�r�z NCReplica.dumpstr_to_be_modifiedc
Cstz
|j|jtjdgd�}Wntjy(}z|j\}}td|j|f��d}~ww|d}d|vr8|d|_dSdS)z�Given an NC replica which has been discovered thru the nTDSDSA
        database object, load the fSMORoleOwner attribute.
        �
fSMORoleOwnerr1rvNr)r5r r6r7r8r9r
rX)r%r<r=�e3r?r@rArrr�load_fsmo_roles�s
�
����zNCReplica.load_fsmo_rolescCs|jdur|j|krdSdS)NTF)rX�r%rPrrr�is_fsmo_role_owner�s

zNCReplica.is_fsmo_role_ownercCrt)a�Given an NC replica which has been discovered thru the nTDSDSA
        database object, load the repsTo attribute for the local replica.
        held by my dsa.  The repsTo attribute is not replicated so this
        attribute is relative only to the local DSA that the samdb exists on

        This is responsible for push replication, not scheduled pull
        replication. Not to be confused for repsFrom.
        �repsTor1rvNrzbad repsTo NDR: %rrw)r5r r6r7r8r9r
rrryrzr{r|r}r~rZr)r%r<r=�e4r?r@rAr�r�r>rerrr�load_repsTo�s6	
�
�������zNCReplica.load_repsToc	
Cr�)zCommit repsTo to the databaseFTNr�z$Could not set repsTo for (%s) - (%s))rZr�rr�r�rr�r�r6r�r�r r�r�r�r�r8r
)	r%r<r�r�r�r�r�r�r@rrr�
commit_repsTo�r�zNCReplica.commit_repsToN)r�F)rrr
rNr&r.rjrMrnrprrrgr�r�r�r�r�r�r�r�r�rrrrrO�s&
1
?! rOc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zd-dd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,S).�DirectoryServiceAgentcCsF||_d|_d|_d|_d|_d|_d|_d|_i|_i|_	i|_
dS)z�Initialize DSA class.

        Class is subsequently fully populated by calling the load_dsa() method

        :param dsa_dnstr:  DN of the nTDSDSA
        NFr)rPrR�dsa_ivid�	dsa_is_ro�dsa_is_istg�options�dsa_behavior�
default_dnstr�current_rep_table�needed_rep_table�
connect_tabler�rrrr&#s
zDirectoryServiceAgent.__init__cCs�d|jj}|jdur|d|j}|jdur |dt|j�}|jdur.|dt|j�}|d|��d|��d|��d	d
|�	�dd
|�
�dd
|��7}|S)
r'r(Nr]r^z
	dsa_ivid=%sr_z
	gc=%sz	
	istg=%sz
current_replica_table:rcz
needed_replica_table:z
connect_table:)r)rrPrRr*r�rp�is_gc�is_istg�dumpstr_current_replica_table�dumpstr_needed_replica_table�dumpstr_connect_tabler,rrrr.@s2



�
��
��
��
�
zDirectoryServiceAgent.__str__cC�|j�|�Sr�)r��getr$rrr�get_current_replicaWsz)DirectoryServiceAgent.get_current_replicacCs|jp|jS)zAReturns True if dsa is intersite topology generator for it's site)r�r�rCrrrr�Z�zDirectoryServiceAgent.is_istgcCrm)z1Returns True if dsa a read only domain controller)r�rCrrrrp_rqzDirectoryServiceAgent.is_rocC�|jtj@dkr
dSdS)z*Returns True if dsa hosts a global catalogrTF)r�r�DS_NTDSDSA_OPT_IS_GCrCrrrr�c�zDirectoryServiceAgent.is_gccCs|j|krdSdS)z�Is dsa at minimum windows level greater than or equal to (version)

        :param version: Windows version to test against
            (e.g. DS_DOMAIN_FUNCTION_2008)
        TF)r�)r%r�rrr�is_minimum_behavioris
z)DirectoryServiceAgent.is_minimum_behaviorcCr�)z>Whether this allows NTDSConnection translation in its options.rTF)r�r�%DS_NTDSDSA_OPT_DISABLE_NTDSCONN_XLATErCrrr�is_translate_ntdsconn_disabledsr�z4DirectoryServiceAgent.is_translate_ntdsconn_disabledcCs|j|jfS)z5Return DSA current and needed replica tables
        )r�r�rCrrr�get_rep_tablesyr�z$DirectoryServiceAgent.get_rep_tablescCs|j�d�\}}}|S)z(Get the parent DN string of this object.�,)rP�	partition)r%�head�sep�tailrrr�get_parent_dnstr~sz&DirectoryServiceAgent.get_parent_dnstrc
Csgd�}z|j|jtj|d�}Wntjy+}z|j\}}td|j|f��d}~ww|d}t�|�	d|dd��|_
d|vrPt�|�	d|dd��|_d|vr]t|dd�|_
d	|vrot|d	d�d
krod|_nd|_d
|vrt|d
d�|_|�|�|�|�dS)z�Load a DSA from the samdb.

        Prior initialization has given us the DN of the DSA that we are to
        load.  This method initializes all other attributes, including loading
        the NC replica table for this DSA.
        )r/�invocationIDr��msDS-isRODC�msDS-Behavior-Versionr1z&Unable to find nTDSDSA for (%s) - (%s)Nrr/�invocationIdr�r��TRUETFr�)r5rPr6r7r8r9r
rr:r;rRr��intr�r*r�r��load_current_replica_table�load_connection_table)r%r<r4r=�e5r?r@rArrr�load_dsa�s:
�
���

�

�
zDirectoryServiceAgent.load_dsac
Cs(gd�}z|j|jtj|d�}Wntjy+}z|j\}}td|j|f��d}~wwi}t|d�dkr�|d��D]J}|dkrCq<|d|D]<}	t	||	�
d��}
|
��}t|
j
�}||vrjt||�}
|
||<n||}
|dkrx|
�|�qI|
�||�|
��r�||_qIq<ntd	|j��||_dS)
a#Method to load the NC replica's listed for DSA object.

        This method queries the samdb for (hasMasterNCs, msDS-hasMasterNCs,
        hasPartialReplicaNCs, msDS-HasDomainNCs, msDS-hasFullReplicaNCs, and
        msDS-HasInstantiatedNCs) to determine complete list of NC replicas that
        are enumerated for the DSA.  Once a NC replica is loaded it is
        identified (schema, config, etc) and the other replica attributes
        (partial, ro, etc) are determined.

        :param samdb: database to query for DSA replica list
        )rJrlrIrHrk�msDS-HasInstantiatedNCsr1z*Unable to find nTDSDSA NCs for (%s) - (%s)Nrr��utf8r�zNo nTDSDSA NCs for (%s))r5rPr6r7r8r9r
�len�keysr�decode�get_binary_integerr*r�rOrjrMrnr�r�)r%r<�ncattrsr=�e6r?r@�	tmp_tablerr��dsdnri�dnstrrerrrr��sH
�
���



���
z0DirectoryServiceAgent.load_current_replica_tablecCs||j|j<dS)z]Method to add a NC replica that "should be present" to the
        needed_rep_table.
        N)r�r )r%rerrr�add_needed_replicasz(DirectoryServiceAgent.add_needed_replicac	
Cs�z|j|jtjdd�}Wntjy'}z|j\}}td|j|f��d}~ww|D]}t|j�}||j	vr7q*t
|�}|�|�||j	|<q*dS)z�Method to load the nTDSConnections listed for DSA object.

        :param samdb: database to query for DSA connection list
        z(objectClass=nTDSConnection)�r2r3�
expression�-Unable to find nTDSConnection for (%s) - (%s)N)r5rPr6�
SCOPE_SUBTREEr8r9r
r*r�r��NTDSConnection�load_connection)	r%r<r=�e7r?r@rAr��connectrrrr�s(
�
���


�z+DirectoryServiceAgent.load_connection_tableFcCsng}|j��D]$\}}|jr|�||�|jr|�||�|jr+|�||�|�|�q|D]}|j|=q.dS)azMethod to commit any uncommitted nTDSConnections
        modifications that are in our table.  These would be
        identified connections that are marked to be added or
        deleted

        :param samdb: database to commit DSA connection list to
        :param ro: if (true) then peform internal operations but
            do not write to the database (readonly)
        N)	r��items�to_be_added�commit_added�to_be_modified�commit_modifiedr��commit_deletedr)r%r<r��delconnr�r�rrr�commit_connections's

�
�z(DirectoryServiceAgent.commit_connectionscCs||jvsJ�||j|<dSr�)r�)r%r�r�rrr�add_connectionBsz$DirectoryServiceAgent.add_connectioncCs.g}|j��D]
}|��|kr|�|�q|S)z�Scan DSA nTDSConnection table and return connection
        with a "fromServer" dn string equivalent to method
        input parameter.

        :param from_dnstr: search for this from server entry
        )r��values�get_from_dnstrr)r%�
from_dnstr�answerr�rrr�get_connection_by_from_dnstrFs
�z2DirectoryServiceAgent.get_connection_by_from_dnstrcCr�)z1Debug dump string output of current replica tabler�cs��|]}t|�VqdSr��r*r�rrrrVrfzFDirectoryServiceAgent.dumpstr_current_replica_table.<locals>.<genexpr>)rhr�rCrrrr�T�z3DirectoryServiceAgent.dumpstr_current_replica_tablecCr�)z0Debug dump string output of needed replica tabler�csr�r�rr�rrrrZrfzEDirectoryServiceAgent.dumpstr_needed_replica_table.<locals>.<genexpr>)rhr�rCrrrr�Xrz2DirectoryServiceAgent.dumpstr_needed_replica_tablecCr�)z)Debug dump string output of connect tabler�csr�r�rr�rrrr^rfz>DirectoryServiceAgent.dumpstr_connect_table.<locals>.<genexpr>)rhr�rCrrrr�\rz+DirectoryServiceAgent.dumpstr_connect_tablecCs|dtt���|j}t|�}d|_d|_||_||_||_	|dur*|j
|_|j|_
|dur2||_nt�|_|�||�|S)z�Set up a new connection for the DSA based on input
        parameters.  Connection will be added to the DSA
        connect_table and will be marked as "to be added" pending
        a call to commit_connections()
        zCN=%s,TN)r*�uuid�uuid4rPr�r��enabledr�r��system_flagsr��transport_dnstr�guid�transport_guid�schedule�new_connection_scheduler�)r%r�r�	transportr��schedr�r�rrr�new_connection`sz$DirectoryServiceAgent.new_connectionNr�)rrr
r&r.r�r�rpr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r
rrrrr�!s,
0S
r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd*dd�Zd*d
d�Z	d*dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)S)+r�z5Class defines a nTDSConnection found under a DSA
    cCsR||_d|_d|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_dS)NFr)
r�rr�whenCreatedr�r�r�r�rrrr�r	�r%r�rrrr&�s
zNTDSConnection.__init__cCs>d|jj|jfd|jd|jd|jd|jd|jd|jd|j	d	|j
}|jd
ur?|dt|j�7}|j
d
urM|dt|j
�7}|d
|j}|jd
ur�|d|jjd|jjd|jj7}t|jj�D])\}}|d||jfd||jfd|d�dd�|jj|jD��d7}qs|S)z1Debug dump string output of NTDSConnection object�
%s:
	dn=%sz
	enabled=%sz
	to_be_added=%sz
	to_be_deleted=%sz
	to_be_modified=%sz
	options=0x%08Xz
	system_flags=0x%08Xz
	whenCreated=%dz
	transport_dn=%sN�	
	guid=%s�
	transport_guid=%sz
	from_dn=%s�
	schedule.size=%s�
	schedule.bandwidth=%s�
	schedule.numberOfSchedules=%s�"
	schedule.headerArray[%d].type=%d�$
	schedule.headerArray[%d].offset=%d� 
	schedule.dataArray[%d].slots[ r`csra�z0x%X Nr�r�slotrrrr�rfz)NTDSConnection.__str__.<locals>.<genexpr>�])r)rr�rr�r�r�r�rrrrr*rr�r	�size�	bandwidth�numberOfSchedules�	enumerate�headerArray�type�offsetrh�	dataArray�slots)r%r-�i�headerrrrr.�sZ��������




���������zNTDSConnection.__str__c	
Cs�gd�}z|j|jtj|d�}Wntjy+}z|j\}}td|j|f��d}~ww|d}d|vr=t|dd�|_d|vrTt	|dd��
�����dkrTd	|_
d
|vrat|d
d�|_zt�|�d|dd��|_Wntytd|j��wd
|vr�t||d
d�d��}|�|t	|j��d|vr�ttj|dd�|_d|vr�t�t	|dd��|_d|vr�t||dd�d��}t	|j�|_|jdus�J�dSdS)z�Given a NTDSConnection object with an prior initialization
        for the object's DN, search for the DN and load attributes
        from the samdb.
        )r��enabledConnectionr	rr/�
transportType�
fromServer�systemFlagsr1r�Nrr�r(r�Tr+r/z4Unable to find objectGUID in nTDSConnection for (%s)r)r�r	rr*)r5r�r6r7r8r9r
r�r�r*�upper�lstrip�rstriprrrr:r;r�KeyErrorrr��load_connection_transportr�rrr	�string_to_timerr�)	r%r<r4r=�e8r?r@rAr�rrrr��sV
�
��� 

�����zNTDSConnection.load_connectionc	
Cs�dg}z|j|tj|d�}Wntjy(}z
|j\}}td||f��d}~wwd|dvrD|d}||_t�|�	d|dd��|_
|jdusKJ�|j
dusRJ�dS)z�Given a NTDSConnection object which enumerates a transport
        DN, load the transport information for the connection object

        :param tdnstr: transport DN to load
        r/r1z$Unable to find transport (%s) - (%s)Nr)r5r6r7r8r9r
rrr:r;r)	r%r<�tdnstrr4r=�e9r?r@rArrrr0�s*
�
���

��z(NTDSConnection.load_connection_transportFc
Csb|jsJ�d|_|rdSz	|�|j�WdStjy0}z|j\}}td|j|f��d}~ww)z�Local helper routine for commit_connections() which
        handles committed connections that are to be deleted from
        the database database
        FNz/Could not delete nTDSConnection for (%s) - (%s))r��deleter�r6r8r9r
)r%r<r��e10r?r@rrrr�	s

���zNTDSConnection.commit_deletedc
Cs�|jsJ�d|_|rdSd}z|j|jtjd�}t|�dkr d}Wn&tjyG}z|j\}}|tjkr=t	d|j|f��WYd}~nd}~ww|rQt	d|j��|j
rWd}nd	}t��}	t�||j�|	_
t�d
tjd�|	d<t�dtjd�|	d<t�|tjd
�|	d
<t�|jtjd�|	d<t�t|j�tjd�|	d<t�t|j�tjd�|	d<|jdur�t�t|j�tjd�|	d<|jdur�t�t|j�tjd�|	d<z|�|	�WdStjy�}
z|
j\}}t	d|j|f��d}
~
ww)z�Local helper routine for commit_connections() which
        handles committed connections that are to be added to the
        database
        FN�r2r3rT� Unable to search for (%s) - (%s)z'nTDSConnection for (%s) already exists!r��FALSE�nTDSConnection�objectClass�showInAdvancedViewOnlyr(r*r�r+r)r	z,Could not add nTDSConnection for (%s) - (%s))r�r5r�r6r7r�r8r9�ERR_NO_SUCH_OBJECTr
rr�r�r�r��FLAG_MOD_ADDr�r*r�rrr	r�add)r%r<r��foundrA�e11r?r@�	enablestrr��e12rrrr�s�
�

�����
��
��
������
��
��
���zNTDSConnection.commit_addedc	
Cs�|jsJ�d|_|rdSz|j|jtjd�Wn'tjy>}z|j\}}|tjkr1td|j��td|j|f��d}~ww|j	rEd}nd}t�
�}t�||j�|_t�
|tjd�|d<t�
|jtjd	�|d	<t�
t|j�tjd
�|d
<t�
t|j�tjd�|d<|jdur�t�
t|j�tjd�|d<n
t�
gtjd�|d<|jdur�t�
t|j�tjd
�|d
<n
t�
gtjd
�|d
<z|�|�WdStjy�}z|j\}}td|j|f��d}~ww)z�Local helper routine for commit_connections() which
        handles committed connections that are to be modified to the
        database
        FNr7z&nTDSConnection for (%s) doesn't exist!r8r�r9r(r*r�r+r)r	z/Could not modify nTDSConnection for (%s) - (%s))r�r5r�r6r7r8r9r=r
rr�r�r�r�r�r�r*r�rr�FLAG_MOD_DELETEr	rr�)	r%r<r��e13r?r@rBr��e14rrrr�csz


����
��������
���
���
���zNTDSConnection.commit_modifiedcCs
||_dSr�)r�)r%�	truefalserrr�set_modified��
zNTDSConnection.set_modifiedcCsH|jdus
|jjddurdS|jjdjD]}|d@dkr!dSqdS)zzReturns True if our schedule includes at least one
        replication interval within the week.  False otherwise
        NrF�T)r	r$r%)r%rrrr�!is_schedule_minimum_once_per_week�s�z0NTDSConnection.is_schedule_minimum_once_per_weekcCs�|jdur	|duS|durdS|jj|jks$|jj|jks$|jj|jkr&dSt|jj�D]?\}}|jj|j|j|jkr@dS|jj|j|j|jkrPdSt|jj	|j
|j	|j
�D]\}}||krjdSq^q,dS)z�Returns True if our schedule is equivalent to the input
        comparison schedule.

        :param shed: schedule to compare to
        NFT)r	rrrr r!r"r#�zipr$r%)r%rr&r'�a�brrr�is_equivalent_schedule�s.

�
���z%NTDSConnection.is_equivalent_schedulecC�|jtj@dkr
dSdS)zMReturns True if NTDS Connection specifies RODC
        topology only
        rFT)r�r�NTDSCONN_OPT_RODC_TOPOLOGYrCrrr�is_rodc_topology��zNTDSConnection.is_rodc_topologycCrP)z�Returns True if NTDS Connection was generated by the
        KCC topology algorithm as opposed to set by the administrator
        rFT)r�r�NTDSCONN_OPT_IS_GENERATEDrCrrr�is_generated�rSzNTDSConnection.is_generatedcCrP)zGReturns True if NTDS Connection should override notify default
        rFT)r�r�$NTDSCONN_OPT_OVERRIDE_NOTIFY_DEFAULTrCrrr�is_override_notify_default��z)NTDSConnection.is_override_notify_defaultcCrP)z:Returns True if NTDS Connection should use notify
        rFT)r�r�NTDSCONN_OPT_USE_NOTIFYrCrrr�
is_use_notify�rXzNTDSConnection.is_use_notifycCrP)z?Returns True if NTDS Connection should use twoway sync
        rFT)r�r�NTDSCONN_OPT_TWOWAY_SYNCrCrrr�is_twoway_syncrXzNTDSConnection.is_twoway_synccCrP)zRReturns True if NTDS Connection intersite compression
        is disabled
        rFT)r�r�*NTDSCONN_OPT_DISABLE_INTERSITE_COMPRESSIONrCrrr�!is_intersite_compression_disabledrSz0NTDSConnection.is_intersite_compression_disabledcCrP)zBReturns True if NTDS Connection has a user owned schedule
        rFT)r�r� NTDSCONN_OPT_USER_OWNED_SCHEDULErCrrr�is_user_owned_schedulerXz%NTDSConnection.is_user_owned_schedulecCrm)z3Returns True if NTDS Connection is enabled
        )rrCrrr�
is_enabledrozNTDSConnection.is_enabledcCrm)z%Return fromServer dn string attribute)r�rCrrrr�rqzNTDSConnection.get_from_dnstrNr�)rrr
rNr&r.r�r0r�r�r�rHrKrOrRrUrWrZr\r^r`rar�rrrrr�s*&9


GG(r�c@r)�	Partitiona!A naming context discovered thru Partitions DN of the config schema.

    This is a more specific form of NamingContext class (inheriting from that
    class) and it identifies unique attributes enumerated in the Partitions
    such as which nTDSDSAs are cross referenced for replicas
    cCs.||_d|_d|_g|_g|_t�|d�dS)NTr)�partstrrr�rw_location_list�ro_location_listrr&)r%rcrrrr&(szPartition.__init__c
Cs>gd�}z|j|jtj|d�}Wntjy+}z|j\}}td|j|f��d}~ww|d}|��D]c}|dkr;q4|dkrWt||d��	��
���dkrSd	|_nd
|_q4|dkret
||d�|_q4||D]-}	t||	�d��}
t|
j�}|d
kr�||_qi|dkr�|j�|�qi|dkr�|j�|�qiqiq4|�|�dS)a�Given a Partition class object that has been initialized with its
        partition dn string, load the partition from the sam database, identify
        the type of the partition (schema, domain, etc) and record the list of
        nTDSDSAs that appear in the cross reference attributes
        msDS-NC-Replica-Locations and msDS-NC-RO-Replica-Locations.

        :param samdb: sam database to load partition from
        )�nCName�Enabledr+�msDS-NC-Replica-Locations�msDS-NC-RO-Replica-Locationsr1z(Unable to find partition for (%s) - (%s)Nrr�rgr�TFr+r�rfrhri)r5rcr6r7r8r9r
r�r*r,r-r.rr�rrr�r�r rdrrerG)r%r<r4r=�e15r?r@rArr�r�r�rrr�load_partition4sN	
�
��� 
��zPartition.load_partitioncCrm)z-Returns True if partition is enabled
        )rarCrrrranrozPartition.is_enabledcCrP)z�Returns True if this is not an Active Directory NC in our
        forest but is instead something else (e.g. a foreign NC)
        rTF)rr�SYSTEM_FLAG_CR_NTDS_NCrCrrr�
is_foreignsszPartition.is_foreigncCs�d}d}|jtjkp|jtjkp|jtjko|j|jk}|jtjkr7|��r/|j	|j
vr.d}n|j	|jvr7d}|��rW|jtjkrW|j|jkrW|j	|j
vsS|j	|jvrWd}d}|ra|��s_|rad}|||fS)z�Tests whether this partition should have an NC replica
        on the target dsa.  This method returns a tuple of
        needed=True/False, ro=True/False, partial=True/False

        :param target_dsa: should NC be present on target dsa
        FT)
r#rrrrr r�rrprPrerdr�)r%�
target_dsar��partial�neededrrr�should_be_present|s2
�
�
�
zPartition.should_be_presentcCsHdt�|�d|jd�dd�|jD��d�dd�|jD��}|S)r'z%sz
	partdn=%sr`csra)z
	msDS-NC-Replica-Locations=%sNr�rrrrrr�rfz$Partition.__str__.<locals>.<genexpr>csra)z!
	msDS-NC-RO-Replica-Locations=%sNrrrrrrr�rf)rr.rcrhrdrer,rrrr.�s���zPartition.__str__N)
rrr
rNr&rkrarmrqr.rrrrrb!s:	0rbc@sxeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�Sitez�An individual site object discovered thru the configuration
    naming context.  Contains all DSAs that exist within the site
    cCs4||_d|_d|_d|_d|_i|_i|_||_dS�Nr)�
site_dnstr�	site_guid�site_options�site_topo_generator�site_topo_failover�	dsa_table�rw_dsa_table�nt_now)r%rur|rrrr&�s
z
Site.__init__c

Csd|j}gd�}z|j|tj|d�}|j|jtjdgd�}Wntjy9}z
|j\}}td||f��d}~ww|d}	d|	vrKt|	dd�|_d	|	vrXt	|	d	d�|_
d
|	vret|	d
d�|_|d}	d|	vr{t�
|�d|	dd��|_|�|�dS)z�Loads the NTDS Site Settings options attribute for the site
        as well as querying and loading all DSAs that appear within
        the site.
        �CN=NTDS Site Settings,%s)r��interSiteTopologyFailover�interSiteTopologyGeneratorr1r/z,Unable to find site settings for (%s) - (%s)Nrr�rr~)rur5r6r7r8r9r
r�rwr*rxryrr:r;rv�load_all_dsa)
r%r<�ssdnr4r=�self_res�e16r?r@rArrr�	load_site�s<

�
�
����

�zSite.load_sitec	
Cs�z|j|jtjdd�}Wntjy$}z|j\}}td|��d}~ww|D]$}t|j�}||j	vr4q't
|�}|�|�||j	|<|��sK||j
|<q'dS)z�Discover all nTDSDSA thru the sites entry and
        instantiate and load the DSAs.  Each dsa is inserted
        into the dsa_table by dn string.
        z(objectClass=nTDSDSA))r3r�zUnable to find nTDSDSAs - (%s)N)r5rur6r�r8r9r
r*r�rzr�r�rpr{)	r%r<r=�e17r?r@rAr�r\rrrr��s*
�
��




��zSite.load_all_dsacCr�)z�Return a previously loaded DSA object by consulting
        the sites dsa_table for the provided DSA dn string

        :return: None if DSA doesn't exist
        )rzr�rrrr�get_dsaszSite.get_dsac
Cs�|jrd|_|j|_dSt|�}|�|�|�|�t|j�	�dd�d�}|j
dkr-d}n|j
dd}|j�|j�}|d	urt||urt|�
|�}d
}	|jD]}
|j|
jkrYd}	nqM|	sa|}d}n|j|
j|krnd}d}n|}|
j}n|�
|�}|j}||j||t|�}
||
}d|_||ur�d
S|j|jkr�dS|j|_|r�dSd|j}t��}t�||�|_t�|jtjd�|d<z|�|�WdStjy�}ztd
||f��d	}~ww)alDetermine if my DC should be an intersite topology
        generator.  If my DC is the istg and is both a writeable
        DC and the database is opened in write mode then we perform
        an originating update to set the interSiteTopologyGenerator
        attribute in the NTDS Site Settings object.  An RODC always
        acts as an ISTG for itself.
        TcSs
t|j�Sr�)rrR)r\rrr�<lambda>5s
z"Site.select_istg.<locals>.<lambda>)�keyrlPC�<i���NFr}rz8Could not set interSiteTopologyGenerator for (%s) - (%s))r�r�rPrx�get_dsa_config_repr�r��sortedr{r�ryrzr��indexr�r��source_dsa_invocation_idr|�last_sync_successr�rur6r�r�r�r�r�r�r8r
)r%r<�mydsar��c_rep�D_sort�f�d_dsa�j_idxr@�cursor�i_idx�t_time�k_idxr�r�r@rrr�select_istgsz	

�


�


�������zSite.select_istgcC�|jtj@dkS)z8Returns True if intra-site topology is disabled for siter)rwr�-DS_NTDSSETTINGS_OPT_IS_AUTO_TOPOLOGY_DISABLEDrCrrr�is_intrasite_topology_disabled��
��z#Site.is_intrasite_topology_disabledcCr�)z8Returns True if inter-site topology is disabled for siter)rwr�8DS_NTDSSETTINGS_OPT_IS_INTER_SITE_AUTO_TOPOLOGY_DISABLEDrCrrr�is_intersite_topology_disabled�s
��z#Site.is_intersite_topology_disabledcCr�)z:Returns True if selection of random bridgehead is disabledr)rwr�1DS_NTDSSETTINGS_OPT_IS_RAND_BH_SELECTION_DISABLEDrCrrr�is_random_bridgehead_disabled�r�z"Site.is_random_bridgehead_disabledcCr�)z1Returns True if detect stale is disabled for siter)rwr�1DS_NTDSSETTINGS_OPT_IS_TOPL_DETECT_STALE_DISABLEDrCrrr�is_detect_stale_disabled�r�zSite.is_detect_stale_disabledcCr�)z<Returns True if NTDS Connection cleanup is disabled for siter)rwr�,DS_NTDSSETTINGS_OPT_IS_TOPL_CLEANUP_DISABLEDrCrrr�is_cleanup_ntdsconn_disabled�r�z!Site.is_cleanup_ntdsconn_disabledcCs|�|j�rdSdS)z"Return True if dsa is in this siteTF)r�rP)r%r\rrr�	same_site�szSite.same_sitecCs$t|j�dkrt|j�dkrdSdS)NrTF)r�rzr{rCrrr�is_rodc_site�szSite.is_rodc_sitecCsXd|jjd|jd|jd|jd|j}|j��D]
\}}|d|}q|S)r'r(z
	dn=%sz
	options=0x%Xz
	topo_generator=%sz
	topo_failover=%drc)r)rrurwrxryrzr�)r%r-r�r\rrrr.�s
����zSite.__str__N)rrr
rNr&r�r�r�r�r�r�r�r�r�r�r�r.rrrrrs�s 
%*rsc@r)�	GraphNodez�A graph node describing a set of edges that should be directed to it.

    Each edge is a connection for a particular naming context replica directed
    from another node in the forest to this node.
    cCs||_||_g|_dS)z�Instantiate the graph node according to a DSA dn string

        :param max_node_edges: maximum number of edges that should ever
            be directed to the node
        N)�	max_edgesrP�	edge_from)r%rP�max_node_edgesrrrr&�s
zGraphNode.__init__cCsRd|jjd|jd|j}t|j�D]\}}t|t�r&|d||f7}q|S)Nr(r]z
	max_edges=%dz
	edge_from[%d]=%s)r)rrPr�r r��
isinstancer*)r%r-r&�edgerrrr.�s
��
�zGraphNode.__str__cCsNt|t�sJ�||jkrdS||jvrdSt|j�|jkrdS|j�|�dS)z�Add an edge from the dsa to our graph nodes edge from list

        :param from_dsa_dnstr: the dsa that the edge emanates from
        FT)r�r*rPr�r�r�r)r%�from_dsa_dnstrrrr�
add_edge_froms

zGraphNode.add_edge_fromcCs |j��D]}|�|j�qdS)aeFor each nTDSConnection object associated with a particular
        DSA, we test if it implies an edge to this graph node (i.e.
        the "fromServer" attribute).  If it does then we add an
        edge from the server unless we are over the max edges for this
        graph node

        :param dsa: dsa with a dnstr equivalent to his graph node
        N)r�r�r�r�)r%r\r�rrr�add_edges_from_connectionss	�z$GraphNode.add_edges_from_connectionsc	Cs`|jD]*}|�|�}d}|D]	}|��rqd}q|rqtj}tjtjB}|�||||d�qdS)z�For each edge directed to this graph node, ensure there
           is a corresponding nTDSConnection object in the dsa.
        FTN)r�r�rRrrT�SYSTEM_FLAG_CONFIG_ALLOW_RENAME�SYSTEM_FLAG_CONFIG_ALLOW_MOVEr
)	r%r\r�
edge_dnstr�connections�found_validr��optrirrr�add_connections_from_edges#s

��z$GraphNode.add_connections_from_edgescCst|j�|jkr
dSdS)z<Return True if we have met the maximum "from edges" criteriaTF)r�r�r�rCrrr�has_sufficient_edgesJr�zGraphNode.has_sufficient_edgesN)
rrr
rNr&r.r�r�r�r�rrrrr��s
'r�c@�(eZdZdZdd�Zdd�Zdd�ZdS)	�	Transportz;Class defines a Inter-site transport found under Sites
    cCs(||_d|_d|_d|_d|_g|_dSrt)r�r�r�name�address_attr�bridgehead_listrrrrr&Us
zTransport.__init__cCsXd|jj|jfdt|j�d|jd|jd|jd�dd�|j	D��}|S)	�,Debug dump string output of Transport objectrr�
	options=%dz
	address_attr=%sz	
	name=%sr`csra)z
	bridgehead_list=%sNr)rr�rrrrerfz$Transport.__str__.<locals>.<genexpr>)
r)rr�r*rr�r�r�rhr�r,rrrr.]s�����zTransport.__str__c
Csgd�}z|j|jtj|d�}Wntjy+}z|j\}}td|j|f��d}~ww|d}t�|�	d|dd��|_
d|vrKt|dd�|_d|vrXt
|dd�|_d	|vret
|d	d�|_d
|vr�|d
D]}t||�d��}	t
|	j�}
|
|jvr�|j�|
�qmdSdS)z�Given a Transport object with an prior initialization
        for the object's DN, search for the DN and load attributes
        from the samdb.
        )r/r�r��bridgeheadServerListBL�transportAddressAttributer1z(Unable to find Transport for (%s) - (%s)Nrr/r�r�r�r�r�)r5r�r6r7r8r9r
rr:r;rr�r�r*r�r�rr�r�r�r)r%r<r4r=�e18r?r@rAr�r�r�rrr�load_transportis>
�
���

�

��zTransport.load_transportN)rrr
rNr&r.r�rrrrr�Qs
r�c@sBeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)r~z�Class encapsulation of the NDR repsFromToBlob.

    Removes the necessity of external code having to
    understand about other_info or manipulation of
    update flags.
    NcCs�d|jd<||jd<d|jd<|dur:t��|jd<d|jd_d|jd<d|jd	<t��|jdj_|jd
<dS||jd<|jj|jd
<|jdkrZ|jjj|jd<d|jd	<dS|jjj|jd<|jjj	|jd	<dS)NFr�r r�update_flagsr���	dns_name1�	dns_name2�
other_info)
�__dict__rryr��repsFromTo1OtherInfor�r��dns_namer�r�)r%r r�rrrr&�s$




�

zRepsFromTo.__init__cCs�d|jjd|jd|jd|jd|jd|jd|jd|jd	|j	d
|j
d|jd|jd
|j
dd�dd�|jD��d}|S)r'r(z

	dnstr=%sz
	update_flags=0x%Xz
	version=%dz
	source_dsa_obj_guid=%sz
	source_dsa_invocation_id=%srz
	replica_flags=0x%Xz
	consecutive_sync_failures=%dz
	last_success=%sz
	last_attempt=%sz
	dns_name1=%sz
	dns_name2=%sz
	schedule[ r`csrarrrrrrr�rfz%RepsFromTo.__str__.<locals>.<genexpr>r)r)rr r�r��source_dsa_obj_guidr�r�
replica_flags�consecutive_sync_failures�last_success�last_attemptr�r�rhr	r,rrrr.�sH
��������	����
�����zRepsFromTo.__str__cCsL|dvr,|dvr|jdtjO<n|dvr!|jdtjO<t|jdj||�nn|dvrU||jd<|jdjdkrI|jd|jdjj_nQ|jd|jdjj_	nE|d	vrv||jd
<|jdjdkrjt
|��|jd
|jdjj_n$|dvr�||jd<n|d
vr�||jd<n|dvr�t
d|��t
d|��|jdtjO<dS)z�Set an attribute and chyange update flag.

        Be aware that setting any RepsFromTo attribute will set the
        drsuapi.DRSUAPI_DRS_UPDATE_ADDRESS update flag.
        �r	r�rr�r�r�r�r�)r�r�)r	r��r�r�r��r�r��r r �r�r��r�z$Attempt to set readonly attribute %s�Unknown attribute %sN)
r�r�DRSUAPI_DRS_UPDATE_FLAGS�DRSUAPI_DRS_UPDATE_SCHEDULE�setattrr�r�r�r�r�r�r��DRSUAPI_DRS_UPDATE_ADDRESS)r%�itemr�rrr�__setattr__�s6
��
�zRepsFromTo.__setattr__cCs�|dvr
t|jdj|�S|dvr|jdjS|dvr3|jdjdkr+|jdjjjS|jdjjjS|dvrK|jdjdkrCt|��|jdjjjS|dvrT|jdS|d	vr]|jd
S|dvrf|jdStd
|��)zzOverload of RepsFromTo attribute retrieval.

        Allows external code to ignore substructures within the blob
        r�r�r�r�r�r�r�r�r�r �r�r�r�)	�getattrr�r�r�r�r�r�r�r�)r%r�rrr�__getattr__s&


zRepsFromTo.__getattr__cCs
|jdkSrtr�rCrrrr�;rIzRepsFromTo.is_modifiedcCsd|jd<dS)Nrr�)r�rCrrrr�>szRepsFromTo.set_unmodified)NN)
rrr
rNr&r.r�r�r�r�rrrrr~�s
42%r~c@r�)	�SiteLinkz0Class defines a site link found under sites
    cCs.||_d|_d|_d|_d|_d|_g|_dSrt)r�r�r�costr	�interval�	site_listrrrrr&Fs
zSiteLink.__init__cCs�d|jj|jfd|jd|jd|jd|j}|jdurf|d|jjd|jj	d	|jj
7}t|jj�D])\}}|d
||j
fd||jfd|d
�dd�|jj|jD��d7}q<|jD]\}}|d||f}qi|S)r�rr�z
	system_flags=%dz	
	cost=%dz
	interval=%sNrrrrrrr`csrarrrrrrrdrfz#SiteLink.__str__.<locals>.<genexpr>rz
	site_list=%s (%s))r)rr�r�rr�r�r	rrrr r!r"r#rhr$r%r�)r%r-r&r'rr�rrrr.OsD����

���������zSiteLink.__str__c
CsRgd�}z|j|jtj|dgd�}Wntjy-}z|j\}}td|j|f��d}~ww|d}d|vr?t|dd�|_d|vrLt|dd�|_	d	|vrYt|d	d�|_
d
|vrft|d
d�|_d|vr�|dD]'}t||�
d��}	t�|	j�d
��}
t|	j�}|
|f|jvr�|j�|
|f�qnd|vr�ttj|�|_dSt�|_dS)z�Given a siteLink object with an prior initialization
        for the object's DN, search for the DN and load attributes
        from the samdb.
        )r�r+r�r	�replInterval�siteListz
extended_dn:0)r2r3r4�controlsz'Unable to find SiteLink for (%s) - (%s)Nrr�r+r�r�r�r�r:r	)r5r�r6r7r8r9r
r�r�rr�r�rr�rr:r��get_extended_componentr*r�rrrr	r
)r%r<r4r=�e19r?r@rAr�r�rr�rrr�
load_sitelinkks@
�
���
�zSiteLink.load_sitelinkN)rrr
rNr&r.r�rrrrr�Bs
	r�c@seZdZdd�ZdS)�KCCFailedObjectcCs"||_||_||_||_||_dSr�)r�
failure_count�time_first_failure�last_resultr�)r%rr�r�r�r�rrrr&�s

zKCCFailedObject.__init__N)rrr
r&rrrrr��sr�cCs.|j��D]
}|��r|Sqtd|j��)Nz)Unable to find config NC replica for (%s))r�r�rDr
rP)r\r�rrrr��s��r�cCsVt��}d|_d|_d|_t��}d|_d|_|g|_t�	�}dgd|_
|g|_|S)z�Create a default schedule for an NTDSConnection or Sitelink. This
    is packed differently from the repltimes schedule used elsewhere
    in KCC (where the 168 nibbles are packed into 84 bytes).
    �rr���)rr	rrr�scheduleHeaderr"r#r!�
scheduleSlotsr%r$)r	r'�datarrrr
�s
r
c	Csg}|j|��tjdd�}|j|��tjdd�}t�}d}|D]!}|j����}||��d7<|���	�|�	�kr@|d7}qt
|�t
|�k�r
g}	|D]}|j��|vr\|	�|�qNd�t�
|�t�
t|�����}
|	D]�}t�
t|j��}|j|��tjdgd�|�d	gd
�}
|j|��tjddgd�|
|�d	gd
�}t
|
�dk�rt
|�dk�rt|
ddd�}t|ddd�}||kr�qod
}|ddD].}t�|t|��}|��}|���	�}|||kr�d}n|||kr�||�	�kr�d}nq�|�r|j��}|�|�	��qo|S)a
    Discover which sites have no DCs and whose lowest single-hop cost
    distance for any link attached to that site is linked to the site supplied.

    We compare the lowest cost of your single-hop link to this site to all of
    those available (if it exists). This means that a lower ranked siteLink
    with only the uncovered site can trump any available links (but this can
    only be done with specific, poorly enacted user configuration).

    If the site is connected to more than one other site with the same
    siteLink, only the largest site (failing that sorted alphabetically)
    creates the DNS records.

    :param samdb database
    :param site_name origin site (with a DC)

    :return a list of sites this site should be covering (for DNS)
    z*(&(objectClass=server)(serverReference=*))r�z(objectClass=site)rr�zCN={},CN=Sites,{}r�z&(&(objectClass=siteLink)(siteList={}))zserver_sort:1:0:cost)r2r3r4r�r�r�z3(&(objectClass=siteLink)(siteList={})(siteList={}))TF)r5rFr6r�r	r��parent�
canonical_str�
get_rdn_value�lowerr�r�format�
binary_encoder*r�r�)r<�	site_name�sites_to_cover�
server_res�site_res�sites_in_use�dc_countrA�site_dn�sites_uncovered�own_site_dn�site�
encoded_dn�	link_res1�	link_res2�cost1�cost2�to_cover�site_val�site_dn_str�site_rdn�site_cover_rdnrrr�uncovered_sites_to_cover�s�
�
��
��
�
��
�
�r)&r|r6r�sambar�samba.dcerpcrrr�samba.samdbr�	samba.ndrrr�collectionsr	�	Exceptionr
�objectr�dictr�r�r+rrOr�r�rbrsr�r�r~r�r�r�r
rrrrr�<module>sJq`%5i@2Y
!