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: //proc/1233/root/usr/lib/python3/dist-packages/samba/__pycache__/samdb.cpython-310.pyc
o

�/a���@s�dZddlZddlZddlZddlZddlZddlZddlmZmZddl	m
Z
mZddlm
Z
mZddlmZddlmZmZddlmZdd	lmZddlZd
Zdd�ZGd
d�de�ZGdd�de�ZGdd�dej�ZGdd�de�ZdS)z(Convenience functions for using the SAM.�N)�dsdb�dsdb_dns)�
ndr_unpack�ndr_pack)�drsblobs�misc)�normalise_int32)�	get_bytes�cmp)�security)�is_ad_dc_built�restructuredTextcCsdS)N�tdb�rrr�-/usr/lib/python3/dist-packages/samba/samdb.py�get_default_backend_store+src@�eZdZdS)�
SamDBErrorN��__name__�
__module__�__qualname__rrrrr.�rc@r)�SamDBNotFoundErrorNrrrrrr1rrc
sjeZdZdZiZiZdddddejddddf
�fdd�	Zd��fdd�	Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Z		d�dd�Zdd�Zdd�Z			d�dd�Zdd�Zd d!�Zdgd"�dfd#d$�Zd%d&�Zdddid'fd(d)�Z									d�d*d+�Z															d�d,d-�Z			d�d.d/�Zd0d1�Z	d�d2d3�Zd�d4d5�Zd6d7�Zd8d9�Z e!e ed:d;�Z"d<d=�Z#d>d?�Z$e!e$e#d@d;�Z%dAdB�Z&	d�dCdD�Z'dEdF�Z(dGdH�Z)dIdJ�Z*dKdL�Z+dMdN�Z,dOdP�Z-dQdR�Z.dSdT�Z/dUdV�Z0dWdX�Z1dYdZ�Z2d[d\�Z3d]d^�Z4d_d`�Z5dadb�Z6dcdd�Z7d�dedf�Z8d�dgdh�Z9didj�Z:dkdl�Z;dmdn�Z<dodp�Z=dqdr�Z>dsdt�Z?	d�dudv�Z@dwdx�ZAdydz�ZBd{d|�ZCd}d~�ZDdd��ZEd�d��ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d�d��ZO�fd�d��ZPd�d��ZQd�d��ZRd�d�d��ZSd�d��ZTd�d��ZUd�d��ZV	d�d�d��ZWd�d��ZXd�d��ZYd�d��ZZd�d��Z[d�d��Z\�Z]S)��SamDBzThe SAM database.NTc	sv||_|	sd}n|dur|dur|��}||_tt|�j|||||||d�|r-t�|�|
dur9t�||
�dSdS)N)�url�lp�modules_dir�session_info�credentials�flags�options)	r�	samdb_urlr�superr�__init__r�_dsdb_set_global_schema�_dsdb_set_am_rodc)�selfrrrrrr r!�
global_schema�auto_connect�am_rodc��	__class__rrr$:s�
�zSamDB.__init__rcsB|jdurtj�|�s|j�|�}||_tt|�j|||d�dS)zconnect to the databaseN)rr r!)	r�os�path�exists�private_pathrr#r�connect)r'rr r!r+rrr1Ps
�z
SamDB.connectcC�
t�|�S)zreturn True if we are an RODC)r�_am_rodc�r'rrrr*Y�
z
SamDB.am_rodccCr2)z%return True if we are an PDC emulator)r�_am_pdcr4rrr�am_pdc]r5zSamDB.am_pdccC�t|���S)zreturn the domain DN)�str�get_default_basednr4rrr�	domain_dna�zSamDB.domain_dncCr8)zreturn the schema partition dn)r9�get_schema_basednr4rrr�	schema_dner<zSamDB.schema_dncCstjj}|j||dd�dS)z|Disables an account

        :param search_filter: LDAP filter to find the user (eg
            samccountname=name)
        T��onN)�sambar�UF_ACCOUNTDISABLE�toggle_userAccountFlags�r'�
search_filterr rrr�disable_accountiszSamDB.disable_accountcCs$tjjtjjB}|j||dd�dS)z{Enables an account

        :param search_filter: LDAP filter to find the user (eg
            samccountname=name)
        Fr?N)rArrB�UF_PASSWD_NOTREQDrCrDrrr�enable_accountsszSamDB.enable_accountFcCs�|j|��tj|dgd�}t|�dkrtd|��t|�dks!J�|dj}t|ddd�}|rE|r@||@r@d|}	t|	��||B}
n|rS||@sSd|}	t|	��||@}
||
kr^dSd	|||
f}|�|�dS)
anToggle_userAccountFlags

        :param search_filter: LDAP filter to find the user (eg
            samccountname=name)
        :param flags: samba.dsdb.UF_* flags
        :param on: on=True (default) => set, on=False => unset
        :param strict: strict=False (default) ignore if no action is needed
                 strict=True raises an Exception if...
        �userAccountControl��base�scope�
expression�attrsrz!Unable to find account where '%s'�z Account flag(s) '%s' already setz"Account flag(s) '%s' already unsetNz|
dn: %s
changetype: modify
delete: userAccountControl
userAccountControl: %u
add: userAccountControl
userAccountControl: %u
)	�searchr;�ldb�
SCOPE_SUBTREE�len�	Exception�dn�int�modify_ldif)r'rEr �	flags_strr@�strict�res�
account_dn�old_uac�error�new_uac�modrrrrC}s.�


�zSamDB.toggle_userAccountFlagscCs`|j|��tj|gd�}t|�dkrtd|��t|�dks J�|dj}d|}|�|�dS)z�Forces a password change at next login

        :param search_filter: LDAP filter to find the user (eg
            samccountname=name)
        rJr�Unable to find user "%s"rOz=
dn: %s
changetype: modify
replace: pwdLastSet
pwdLastSet: 0
N)rPr;rQrRrSrTrUrW�r'rErZ�user_dnr_rrr�#force_password_change_at_next_login�s�
�z)SamDB.force_password_change_at_next_logincCsh|j|��tj|gd�}t|�dkrtd|��t|�dkr$td|��|dj}d|}|�|�dS)z�Unlock a user account by resetting lockoutTime to 0.
        This does also reset the badPwdCount to 0.

        :param search_filter: LDAP filter to find the user (e.g.
            sAMAccountName=username)
        rJrr`rOzUser "%s" is not uniquez?
dn: %s
changetype: modify
replace: lockoutTime
lockoutTime: 0
N)	rPr;rQrRrSrrrUrWrarrr�unlock_account�s
�
�zSamDB.unlock_accountc
Cs�|rd|||��f}
n
d||�|��tj�f}
|
|dd�}|dur)t|�|d<|dur1||d<|dur9||d<|durA||d	<|durKt|�|d
<|	durW||d<|	|d<|durat|�|d
<|�|�dS)a�Adds a new group with additional parameters

        :param groupname: Name of the new group
        :param grouptype: Type of the new group
        :param description: Description of the new group
        :param mailaddress: Email address of the new group
        :param notes: Notes of the new group
        :param gidnumber: GID Number of the new group
        :param nisdomain: NIS Domain Name of the new group
        :param sd: security descriptor of the object
        �CN=%s,%s,%s�CN=%s,%s�group�rU�sAMAccountName�objectClassN�	groupType�description�mail�info�	gidNumber�msSFU30Name�msSFU30NisDomain�nTSecurityDescriptor)r;�get_wellknown_dnr:r�DS_GUID_USERS_CONTAINERrr�add)r'�	groupname�groupou�	grouptyperl�mailaddress�notes�sd�	gidnumber�	nisdomain�group_dn�
ldbmessagerrr�newgroup�s4��zSamDB.newgroupcC�dt�|�d|��f}|��z*|j|��tj|gd�}t|�dkr)td|��t|�dks1J�|�|dj	�Wn|�
��|��dS)zLDeletes a group

        :param groupname: Name of the target group
        �,(&(sAMAccountName=%s)(objectCategory=%s,%s))�#CN=Group,CN=Schema,CN=ConfigurationrJr�Unable to find group "%s"rON�rQ�
binary_encoder;�transaction_startrPrRrSrT�deleterU�transaction_cancel�transaction_commit)r'rv�groupfilter�targetgrouprrr�deletegroup��zSamDB.deletegroupcCsd}gd�}d|vr|}|D]}||vrtd|d�|�f��qd|vr0|dt�|�tjf7}d|vr=|d	t�|�7}d
|vrY|}|ddkrMd
|}|dtjt�|�f7}d|vru|}|ddkrid
|}|dtjt�|�f7}d|vr�|dt�|�7}d|}|S)N�)�userrg�computer�serviceaccount�contact�allz;Invalid group member type "%s". Valid types are %s and all.z, r�z)(&(sAMAccountName=%s)(samAccountType=%d))rgzQ(&(sAMAccountName=%s)(objectClass=group)(!(groupType:1.2.840.113556.1.4.803:=1)))r�����$�%s$zW(&(samAccountType=%d)(!(objectCategory=msDS-ManagedServiceAccount))(sAMAccountName=%s))r�zT(&(samAccountType=%d)(objectCategory=msDS-ManagedServiceAccount)(sAMAccountName=%s))r�z3(&(objectCategory=Person)(!(objectSid=*))(name=%s))z(|%s))rT�joinrQr�r�ATYPE_NORMAL_ACCOUNT�ATYPE_WORKSTATION_TRUST)r'�member�member_types�filter�all_member_types�member_type�samaccountnamerrr�group_member_filter!sT���������zSamDB.group_member_filter)r�rgr�cCs�dt�|�d|��f}|���z0|j|��tj|dgd�}t|�dkr+td|��t|�dks3J�d}d	t|dj	�}	|D]�}
d
}|d
urL|��}z
t
�|
�}dt|�}Wntyk}
zWYd
}
~
nd
}
~
ww|d
ur�zt�
||
�}|��|�d�kr�|�|�}n|}|�d�}Wnty�}
zWYd
}
~
nd
}
~
ww|d
ur�|�|
|�}|j|tj|gd�}t|�dkr�d}|D]}|d
|�d�7}q�td|
|f��t|�dkr�td|
��|dj	�d�}|du�r|d�d�d
u�st|�dd�|ddD�v�rd}|	dt|�7}	q@|du�r5|d�d�d
u�r5t|�|ddv�r5d}|	dt|�7}	q@|du�r@|�|	�Wn|���|��d
S)z�Adds or removes group members

        :param groupname: Name of the target group
        :param members: list of group members
        :param add_members_operation: Defines if its an add or remove
            operation
        r�r�r�rJrr�rOFz
dn: %s
changetype: modify
Nz<SID=%s>r�z%s
rUz#Found multiple results for "%s":
%sz)Unable to find "%s". Operation cancelled.TcSsg|]}t|��qSr)r9)�.0�xrrr�
<listcomp>�sz2SamDB.add_remove_group_members.<locals>.<listcomp>zadd: member
member: %s
zdelete: member
member: %s
)rQr�r;r�rPrRrSrTr9rUr�dom_sid�	TypeError�Dn�get_linearized�extended_str�normalize_dn_in_domain�
ValueErrorr��getr	rWr�r�)r'rv�members�add_members_operationr��member_base_dnr�r��modified�addtargettogroupr��targetmember_dn�	membersid�e�	member_dn�full_member_dnr��targetmember�targetmemberlist_str�msgrrr�add_remove_group_membersVs����
������>�4��

�zSamDB.add_remove_group_memberscCs||durdS||vrt|�t||�krdSt|�dkr/||vr-t�gtj|�}|�|�dSt�|tj|�}|�|�dS)aAChanges the MessageElement with the given attr_name of the
        given Message. If the value is "" set an empty value and the flag
        FLAG_MOD_DELETE, otherwise set the new value and FLAG_MOD_REPLACE.
        If the value is None or the Message contains the attr_name with this
        value, nothing will changed.Nr)r9rSrQ�MessageElement�FLAG_MOD_DELETEru�FLAG_MOD_REPLACE)r'r��old�	attr_name�value�elrrr�prepare_attr_replace�s
zSamDB.prepare_attr_replacer�cCs�|||d�}|��D]\}}|dkr||vrt||�||<q
|dr2|d�d�s2|dd7<ttd|����}	d�|	�}
|
dkrF|S|
S)z�Prepares new combined fullname, using the name parts.
        Used for things like displayName or cn.
        Use the original name values, if no new one is specified.)�	givenName�initials�snNr��.� r�)�itemsr9�endswith�listr��valuesr�)r'�
given_namer��surname�	old_attrs�fallback_defaultrNr��
attr_value�attrs_values�fullnamerrr�fullname_from_names�s��
zSamDB.fullname_from_namesc&
Cs�|j|||d�}|} |dur|dkr|} |r d| ||��f}!n
d| |�|��tj�f}!t�||������	dd�}"d||"f}#|!||#dd	�}$|rVt
tjtjB�|$d
<d}|dur^||$d<|durf||$d
<|dkrr||$d<||$d<|dur|d||$d<|	dur�|	|$d<|
dur�|
|$d<|dur�||$d<|dur�||$d<|
dur�|
|$d<|dur�||$d<|dur�||$d<|dur�||$d<|dur�||$d<|dur�||$d<|dur�||$d<|dur�||$d<|dur�t
|�|$d<d}%ttdd �|||||||f���r�t��}%t�||!�|%_|du�rt�t
|�tjd!�|%d!<|du�r&t�t
|�tjd"�|%d"<|du�r7t�t
|�tjd#�|%d#<|du�rHt�t
|�tjd$�|%d$<|du�rYt�t
|�tjd%�|%d%<|du�rjt�t
|�tjd&�|%d&<|du�r�t�t
|�tjd'�|%d'<t�t
|�tjd(�|%d(<t�d)tjd*�|%d*<|��z|�|$�|%�r�|�|%�|�r�|�d+t�|!�||�Wn|���|��dS),alAdds a new user with additional parameters

        :param username: Name of the new user
        :param password: Password for the new user
        :param force_password_change_at_next_login_req: Force password change
        :param useusernameascn: Use username as cn rather that firstname +
            initials + lastname
        :param userou: Object container (without domainDN postfix) for new user
        :param surname: Surname of the new user
        :param givenname: First name of the new user
        :param initials: Initials of the new user
        :param profilepath: Profile path of the new user
        :param scriptpath: Logon script path of the new user
        :param homedrive: Home drive of the new user
        :param homedirectory: Home directory of the new user
        :param jobtitle: Job title of the new user
        :param department: Department of the new user
        :param company: Company of the new user
        :param description: of the new user
        :param mailaddress: Email address of the new user
        :param internetaddress: Home page of the new user
        :param telephonenumber: Phone number of the new user
        :param physicaldeliveryoffice: Office location of the new user
        :param sd: security descriptor of the object
        :param setpassword: optionally disable password reset
        :param uidnumber: RFC2307 Unix numeric UID of the new user
        :param gidnumber: RFC2307 Unix primary GID of the new user
        :param gecos: RFC2307 Unix GECOS field of the new user
        :param loginshell: RFC2307 Unix login shell of the new user
        :param uid: RFC2307 Unix username of the new user
        :param nisdomain: RFC2307 Unix NIS domain of the new user
        :param unixhome: RFC2307 Unix home directory of the new user
        :param smartcard_required: set the UF_SMARTCARD_REQUIRED bit of the new user
        �r�r�r�Nr�rerf�/z%s@%sr�)rUri�userPrincipalNamerjrIFr�r��displayName�name�%s.r��profilePath�
scriptPath�	homeDrive�
homeDirectory�title�
department�companyrlrm�wWWHomePage�telephoneNumber�physicalDeliveryOfficeNamerrcSs|duS�Nr)�brrr�<lambda>dszSamDB.newuser.<locals>.<lambda>�uid�	uidNumberro�gecos�
loginShell�unixHomeDirectoryrqrpzABCD!efgh12345$67890�unixUserPassword�(distinguishedName=%s))r�r;rsr:rrtrQr��
canonical_str�replacer9�UF_NORMAL_ACCOUNT�UF_SMARTCARD_REQUIREDr�any�map�MessagerUr�r�r�ru�modify�setpasswordr�r�r�)&r'�username�password�'force_password_change_at_next_login_req�useusernameascn�userour��	givennamer��profilepath�
scriptpath�	homedrive�
homedirectory�jobtitler�r�rlry�internetaddress�telephonenumber�physicaldeliveryofficer{r��	uidnumberr|r��
loginshellr�r}�unixhome�smartcard_required�displayname�cnrb�	dnsdomain�user_principal_namer�ldbmessage2rrr�newuser�s�-���
�
�





�
���

���z
SamDB.newusercCsB|j|||d�}|dur|}|dur|dkrtd��|��}|r%|�|�}d||f}|dd�}|dur8||d<|dur@||d	<|durH||d
<|durRd||d<|durZ||d
<|durb||d<|	durj|	|d<|
durr|
|d<|durz||d<|dur�||d<|
dur�|
|d<|dur�||d<|dur�||d<|�|�|S)a�Adds a new contact with additional parameters

        :param fullcontactname: Optional full name of the new contact
        :param ou: Object container for new contact
        :param surname: Surname of the new contact
        :param givenname: First name of the new contact
        :param initials: Initials of the new contact
        :param displayname: displayName of the new contact
        :param jobtitle: Job title of the new contact
        :param department: Department of the new contact
        :param company: Company of the new contact
        :param description: Description of the new contact
        :param mailaddress: Email address of the new contact
        :param internetaddress: Home page of the new contact
        :param telephonenumber: Phone number of the new contact
        :param mobilenumber: Primary mobile number of the new contact
        :param physicaldeliveryoffice: Office location of the new contact
        r�Nr�zNo name for contact specifiedrfr��rUrjr�r�r�r�r�r�r�r�rlrmr�r��mobiler�)r�rTr;r�ru)r'�fullcontactname�our�r�r�r	rr�r�rlryrr�mobilenumberrr
�contactcontainer_dn�
contact_dnrrrr�
newcontact�sV$�
�
zSamDB.newcontactcCst�dd|�}|�d�rtd|��d|}|�|��tj�}	|r&|�|�}	d||	f}
|
|dd�}|d	ur:||d
<|r@||d<t	tj
tjB�}|rOt	tj
�}||d<|r_d
�||�
��|d<|��z|�|�|r{|��}
|�dt�|
�|
d�Wn|���|��d	S)a�Adds a new user with additional parameters

        :param computername: Name of the new computer
        :param computerou: Object container for new computer
        :param description: Description of the new computer
        :param prepare_oldjoin: Preset computer password for oldjoin mechanism
        :param ip_address_list: ip address list for DNS A or AAAA record
        :param service_principal_name_list: string list of servicePincipalName
        z\$$r�r�zIllegal computername "%s"r�rfr�rhNrl�servicePrincipalNamerIz{}.{}�dNSHostNamer�F)�re�sub�countrTrsr:r�DS_GUID_COMPUTERS_CONTAINERr�r9�UF_WORKSTATION_TRUST_ACCOUNTrB�format�domain_dns_namer�ru�lowerr�rQr�r�r�)r'�computername�
computerourl�prepare_oldjoin�ip_address_list�service_principal_name_listr
r��computercontainer_dn�computer_dnr�accountcontrolr�rrr�newcomputer�sV

�
��
�
���zSamDB.newcomputercCr�)zIDeletes a user

        :param username: Name of the target user
        r�z$CN=Person,CN=Schema,CN=ConfigurationrJrr`rONr�)r'r�r��targetrrr�
deleteuser3r�zSamDB.deleteuserc	Cs�|��zj|j|��tj|gd�}t|�dkrtd|p|��t|�dkr/tdt|�|f��|dj}t|t	�s?|�
d�}n|}d|d�d�}d	|t�
|��
d�f}|�|�|rh|�d
t	|�d�|�|�Wn|���|��dS)
z�Sets the password for a user

        :param search_filter: LDAP filter to find the user (eg
            samccountname=name)
        :param password: Password for the user
        :param force_change_at_next_login: Force password change
        rJrr`rOz*Matched %u multiple users with filter "%s"�utf-8�"z	utf-16-lez?
dn: %s
changetype: modify
replace: unicodePwd
unicodePwd:: %s
z(distinguishedName=�)N)r�rPr;rQrRrSrTrU�
isinstancer9�decode�encode�base64�	b64encoderWrcrHr�r�)	r'rEr��force_change_at_next_loginr�rZrb�pw�setpwrrrr�Hs6	�

�
�zSamDB.setpasswordc	Cs�|��za|j|��tj|ddgd�}t|�dkrtd|��t|�dks'J�|dj}t|ddd�}t|ddd�}|rI|dB}d}n|d@}t	�
|tt����}d	|||f}|�|�Wn|�
��|��d
S)aSets the account expiry for a user

        :param search_filter: LDAP filter to find the user (eg
            samaccountname=name)
        :param expiry_seconds: expiry time from now in seconds
        :param no_expiry_req: if set, then don't expire password
        rI�accountExpiresrJrr`rOii����zy
dn: %s
changetype: modify
replace: userAccountControl
userAccountControl: %u
replace: accountExpires
accountExpires: %u
N)r�rPr;rQrRrSrTrUrVrA�unix2nttime�timerWr�r�)	r'rE�expiry_seconds�
no_expiry_reqrZrbrIr7�setexprrr�	setexpiryts2�
�	zSamDB.setexpirycC�t�||�dS)z`Change the domain SID used by this LDB.

        :param sid: The new domain sid to use.
        N)r�_samdb_set_domain_sid)r'�sidrrr�set_domain_sid��zSamDB.set_domain_sidcCr2)z&Read the domain SID used by this LDB. )r�_samdb_get_domain_sidr4rrr�get_domain_sid�r5zSamDB.get_domain_sidzSID for the domain)�doccCr>)zoSet the invocation id for this SamDB handle.

        :param invocation_id: GUID of the invocation id.
        N)r�_dsdb_set_ntds_invocation_id)r'�
invocation_idrrr�set_invocation_id�rBzSamDB.set_invocation_idcCr2)zGet the invocation_id id)r�_samdb_ntds_invocation_idr4rrr�get_invocation_id�r5zSamDB.get_invocation_idzInvocation ID GUIDcC�t�||�Sr�)r�_dsdb_get_oid_from_attid�r'�attidrrr�get_oid_from_attid��zSamDB.get_oid_from_attidcC�t�|||�S)zNreturn the attribute ID for a LDAP attribute as an integer as found in DRSUAPI)r�$_dsdb_get_attid_from_lDAPDisplayName)r'�ldap_display_name�is_schema_ncrrr�get_attid_from_lDAPDisplayName�s�z$SamDB.get_attid_from_lDAPDisplayNamecCrK)z6return the syntax OID for a LDAP attribute as a string)r�)_dsdb_get_syntax_oid_from_lDAPDisplayName�r'rSrrr�#get_syntax_oid_from_lDAPDisplayName�r<z)SamDB.get_syntax_oid_from_lDAPDisplayNamecCrK)z8return the systemFlags for a LDAP attribute as a integer)r�*_dsdb_get_systemFlags_from_lDAPDisplayNamerWrrr�$get_systemFlags_from_lDAPDisplayName�r<z*SamDB.get_systemFlags_from_lDAPDisplayNamecCrK)z3return the linkID for a LDAP attribute as a integer)r�%_dsdb_get_linkId_from_lDAPDisplayNamerWrrr�get_linkId_from_lDAPDisplayName�r<z%SamDB.get_linkId_from_lDAPDisplayNamecCrK)z;return the lDAPDisplayName from an integer DRS attribute ID)r�"_dsdb_get_lDAPDisplayName_by_attidrMrrr�get_lDAPDisplayName_by_attid�r<z"SamDB.get_lDAPDisplayName_by_attidcCrK)z�return the attribute name of the corresponding backlink from the name
        of a forward link attribute. If there is no backlink return None)r�'_dsdb_get_backlink_from_lDAPDisplayNamerWrrr�!get_backlink_from_lDAPDisplayName�sz'SamDB.get_backlink_from_lDAPDisplayNamecCr>)z�Set the NTDS Settings DN, as would be returned on the dsServiceName
        rootDSE attribute.

        This allows the DN to be set before the database fully exists

        :param ntds_settings_dn: The new DN to use
        N)r�_samdb_set_ntds_settings_dn)r'�ntds_settings_dnrrr�set_ntds_settings_dn�szSamDB.set_ntds_settings_dncCr2)zGet the NTDS objectGUID)r�_samdb_ntds_objectGUIDr4rrr�
get_ntds_GUID�r5zSamDB.get_ntds_GUIDcC�(|jdtjdgd�}t|ddd�S)z/Get the current time as generalized time stringr��currentTime�rKrLrNr�rPrQ�
SCOPE_BASEr9�r'rZrrr�get_timestr�s
�zSamDB.get_timestrcC�t�|���S)z!Get the current time as UNIX time)rQ�string_to_timerlr4rrr�get_time��zSamDB.get_timecCrm)zGet the current time as NT time)rAr8ror4rrr�
get_nttime�rpzSamDB.get_nttimecCr2)zGet the server site name)r�_samdb_server_site_namer4rrr�server_site_name�r5zSamDB.server_site_namecCrf)z return the DNS name of this hostr�rrhrrirkrrr�
host_dns_name��zSamDB.host_dns_namecC�|��}|���d�dS)z&return the DNS name of the domain rootr�r)r:r��split)r'r;rrrr��zSamDB.domain_dns_namecCsf|��}|��}d||f}|��}|j|tj|d�}z
|ddd��}W|Sty2YdSw)z*return the NetBIOS name of the domain rootz?(&(objectClass=crossRef)(nETBIOSName=*)(ncName=%s)(dnsroot=%s)))rLrMr�nETBIOSNameN)r:r�get_partitions_dnrPrQ�SCOPE_ONELEVELr0�
IndexError)r'r;�dns_namer��
partitions_dnrZ�netbios_domainrrr�domain_netbios_names���zSamDB.domain_netbios_namecCrv)z&return the DNS name of the forest rootr�r)�get_root_basednr�rw)r'�	forest_dnrrr�forest_dns_namerxzSamDB.forest_dns_namecCrKr�)r�_dsdb_load_partition_usn)r'�base_dnrrr�load_partition_usnrPzSamDB.load_partition_usncCs|j|j|d�dS)N)�write_indices_and_attributes)�set_schema_from_ldbrQ)r'�schemar�rrr�
set_schemaszSamDB.set_schemacCst�|||�dSr�)r�_dsdb_set_schema_from_ldb)r'�ldb_connr�rrrr�szSamDB.set_schema_from_ldbcCsd}|�|�dS)Nz@
dn:
changetype: modify
add: schemaUpdateNow
schemaUpdateNow: 1
)rW)r'�ldifrrr�set_schema_update_now!szSamDB.set_schema_update_nowcC�t�|||�S)zBconvert a list of attribute values to a DRSUAPI DsReplicaAttribute)r�_dsdb_DsReplicaAttribute�r'rQrS�
ldif_elementsrrr�dsdb_DsReplicaAttribute*rpzSamDB.dsdb_DsReplicaAttributecCr�)z$normalise a list of attribute values)r�_dsdb_normalise_attributesr�rrr�dsdb_normalise_attributes.rpzSamDB.dsdb_normalise_attributescCs>t|j���dkr
|��|�|�|jvr|j|�|�SdS)z� Get from an attid the associated attribute

        :param attid: The attribute id for searched attribute
        :return: The name of the attribute associated with this id
        rN)rS�
hash_oid_name�keys�_populate_oid_attidrOrMrrr�get_attribute_from_attid2s
zSamDB.get_attribute_from_attidcCs\i|_|jddgddgd�}t|�dkr*|D]}t|�d��}||jt|�d��<qdSdS)z�Populate the hash hash_oid_name.

        This hash contains the oid of the attribute as a key and
        its display name as a value
        zobjectClass=attributeSchema�search_options:1:2�attributeID�lDAPDisplayName)rM�controlsrNrN)r�rPrSr9r�)r'rZr��
strDisplayrrrr�?s���zSamDB._populate_oid_attidcCs�|jd|tjdgdgd�}t|�dkrdSttj|ddd�}|j}t|j�	��dkr2|�
�|jD]}|�|j
�}||jvrR|��|j|��krR|jSq5dS)a[Get the version field trom the replPropertyMetaData for
        the given field

        :param dn: The on which we want to get the version
        :param att: The name of the attribute
        :return: The value of the version field in the replPropertyMetaData
            for the given attribute. None if the attribute is not replicated
        �distinguishedName=%sr��replPropertyMetaData�rMrLr�rNrN)rPrQrRrSrr�replPropertyMetaDataBlob�ctrr�r�r��arrayrOrNr �version)r'rU�attrZ�replr��o�att_oidrrr�"get_attribute_replmetadata_versionOs(

��


�z(SamDB.get_attribute_replmetadata_versioncCs�|jd|tjdgdgd�}t|�dkrdSttj|ddd�}|j}t�	t
t����}d}	t|j�
��dkr=|��|jD]4}
|�|
j�}||jvrt|��|j|��krtd}	|�tj�}||
_||
_t�|���|
_||
_||
_q@|	s�|r�t|j�dkr�t��}
d|
_|�|
j�}|�tj�}||
_||
_t�|���|
_||
_||
_d}	|j}|�|
�|jd	|_||_|	r�t |�}t�!�}|dj"|_"t�#|tj$d�|d<|�%|d
g�dSdS)Nr�r�r�r�rFTiZ	rOz#local_oid:1.3.6.1.4.1.7165.4.3.14:0)&rPrQrRrSrrr�r�rAr8rVr9r�r�r�r�rOrNr �sequence_number�SEQ_NEXTr��originating_change_timer�GUIDrJ�originating_invocation_id�originating_usn�	local_usn�replPropertyMetaData1�appendrrr�rUr�r�r�)r'rUr�r��
addifnotexistrZr�r��now�foundr�r��seq�o2�tab�replBlobr�rrr�"set_attribute_replmetadata_versionmsj
��

�
���z(SamDB.set_attribute_replmetadata_versioncCst�|�dSr�)r�'_dsdb_write_prefixes_from_schema_to_ldbr4rrr�write_prefixes_from_schema��z SamDB.write_prefixes_from_schemacCr2r�)r�_dsdb_get_partitions_dnr4rrrrz�s
zSamDB.get_partitions_dncCrKr�)r�_dsdb_get_nc_root)r'rUrrr�get_nc_root�rPzSamDB.get_nc_rootcCsx|j�t|��}d}|dur|�|�}|dur:t�|||�}|dur$|S|dur6i|jt|�<|jt|�}|||<|Sr�)�hash_well_knownr�r9r�_dsdb_get_wellknown_dn)r'�nc_root�wkguid�h_ncrUrrrrs�s
zSamDB.get_wellknown_dncC�Tt|t�st|��d�}t��}t�||���|_t�	|tj
d�|d<|�|�dS)N�utf8�	minPwdAge�r/�bytesr9r1rQr�r�r;rUr�r�r��r'r��mrrr�
set_minPwdAge��
zSamDB.set_minPwdAgecC�L|j|��tjdgd�}t|�dkrdSd|dvrdSt|ddd�S)Nr��rLrNr�rPr;rQrjrSrVrkrrr�
get_minPwdAge��zSamDB.get_minPwdAgecCr�)Nr��	maxPwdAger�r�rrr�
set_maxPwdAge�r�zSamDB.set_maxPwdAgecCr�)Nr�r�rr�rkrrr�
get_maxPwdAge�r�zSamDB.get_maxPwdAgecCr�)Nr��minPwdLengthr�r�rrr�set_minPwdLength�r�zSamDB.set_minPwdLengthcCr�)Nr�r�rr�rkrrr�get_minPwdLength�r�zSamDB.get_minPwdLengthcCr�)Nr��
pwdPropertiesr�r�rrr�set_pwdProperties�r�zSamDB.set_pwdPropertiescCr�)Nr�r�rr�rkrrr�get_pwdProperties�r�zSamDB.get_pwdPropertiescCsbt��}t�|d|�����|_|dur t�|tjd�|d<n
t�gtjd�|d<|�	|�dS)N�1CN=Directory Service,CN=Windows NT,CN=Services,%s�dSHeuristics)
rQr�r��get_config_basednr�rUr�r�r�r�)r'�dsheuristicsr�rrr�set_dsheuristicss
���
��zSamDB.set_dsheuristicscCs\|jd|����tjdgd�}t|�dkrd}|Sd|dvr*|ddd}|Sd}|S)Nr�r�r�r)rPr�r�rQrjrS)r'rZr�rrr�get_dsheuristicss
����zSamDB.get_dsheuristicscCs@|dd�}|r||d<|r||d<|rt|�|d<|�|�dS)aCreates an organizationalUnit object
        :param ou_dn: dn of the new object
        :param description: description attribute
        :param name: name atttribute
        :param sd: security descriptor of the object, can be
        an SDDL string or security.descriptor type
        �organizationalUnitrrlr�rrN)rru)r'�ou_dnrlr�r{r�rrr�	create_ous�zSamDB.create_oucs:|��z
tt|��|�}Wn|���|��|S)z�Returns the value of the sequence number according to the requested type
        :param seq_type: type of sequence number
         )r�r#rr�r�r�)r'�seq_typer�r+rrr�0szSamDB.sequence_numbercCrf)z get the NTDS DN from the rootDSEr��
dsServiceNamerhrrirkrrr�get_dsServiceName>ruzSamDB.get_dsServiceNamecCrf)z"get the server DN from the rootDSEr��
serverNamerhrrirkrrr�get_serverNameCruzSamDB.get_serverNamecCs$|dur
t�||�Stj|||d�S)zDDo a DNS lookup in the database, returns the NDR database structuresN)�
dns_partition)r�lookup)r'r}r�rrr�
dns_lookupHs
�zSamDB.dns_lookupcCrK)z;Return the NDR database structures from a dnsRecord element)r�extract)r'r�rrr�dns_extractPr<zSamDB.dns_extractcCrQ)zfDo a DNS modification on the database, sets the NDR database
        structures on a DNS name
        )rr�)r'r}�new_recordsrrr�dns_replaceTszSamDB.dns_replacecCrQ)z�Do a DNS modification on the database, sets the NDR database
        structures on a LDB DN

        This routine is important because if the last record on the DN
        is removed, this routine will put a tombstone in the record.
        )r�
replace_by_dn)r'rUr�rrr�dns_replace_by_dnZszSamDB.dns_replace_by_dncCs4t�std��|durt�|||�St�||||�S)z�garbage_collect_tombstones(lp, samdb, [dn], current_time, tombstone_lifetime)
        -> (num_objects_expunged, num_links_expunged)z6Cannot garbage collect tombstones: AD DC was not builtN)rrr� _dsdb_garbage_collect_tombstones)r'rU�current_time�tombstone_lifetimerrr�garbage_collect_tombstonescs��z SamDB.garbage_collect_tombstonescCr2)zcreate a RID set for this DSA)r�_dsdb_create_own_rid_setr4rrr�create_own_rid_settr5zSamDB.create_own_rid_setcCr2)z.return a new RID from the RID Pool on this DSA)r�_dsdb_allocate_ridr4rrr�allocate_ridxr5zSamDB.allocate_ridcCs|��\}}|S)aEreturn the next free RID from the RID Pool on this DSA.

        :note: This function is not intended for general use, and care must be
            taken if it is used to generate objectSIDs. The returned RID is not
            formally reserved for use, creating the possibility of duplicate
            objectSIDs.
        )�free_rid_bounds)r'�rid�_rrr�
next_free_rid|szSamDB.next_free_ridcCs6t�||���}|j|tjdgd�}z|dd}Wnty+t�tjd|�d�wt�||d�d��}|j|tjdgd�}z|dd}WntyZt�tjd|�d�wt�||d�d��}|j|tjgd	�d�}d
}d}zt	|ddd�}	Wnty�|}	Ynw|	|kr�t�tj
d
|��zt	|ddd�}
Wnty�|}
Ynwzt	|ddd�}Wnty�|}Ynw|
|ks�||kr�|	}
|
|@}n|d7}|
|@}|
d?}
||
k�r|	|
kr�t�tj
d��|	}
|
|@}|
d?}
|}||k�s||
k�rt�tj
d|||
f��||
fS)a�return the low and high bounds (inclusive) of RIDs that are
            available for use in this DSA's current RID pool.

        :note: This function is not intended for general use, and care must be
            taken if it is used to generate objectSIDs. The returned range of
            RIDs is not formally reserved for use, creating the possibility of
            duplicate objectSIDs.
        �serverReferencerhrzUNo RID Set DN - Cannot find attribute serverReference of %s to calculate reference dnNr,�rIDSetReferenceszVNo RID Set DN - Cannot find attribute rIDSetReferences of %s to calculate reference dn)�rIDAllocationPool�rIDPreviousAllocationPool�
rIDNextRIDl��l����rzBad RID Set %srrrO� zRID pools out of RIDsz"Bad RID chosen %d from range %d-%d)rQr�r�rPrj�KeyError�LdbError�ERR_NO_SUCH_ATTRIBUTEr0rV�ERR_OPERATIONS_ERROR)r'�server_name_dnrZ�
server_ref�
server_ref_dn�rid_set_refs�
rid_set_dn�
uint32_max�
uint64_max�
alloc_pool�	prev_pool�next_rid�prev_pool_lo�prev_pool_hirrrr��s�
���������������

���zSamDB.free_rid_boundscCsHt�||���}t|tj�rt|�}t�||�}|�|�s"|�|�|S)z�return a new DN expanded by adding the domain DN

        If the dn is already a child of the domain DN, just
        return it as-is.

        :param dn: relative dn
        )rQr�r;r/r9�is_child_of�add_base)r'rUr;�full_dnrrrr��s

zSamDB.normalize_dn_in_domain)NrN)NTF)NNNNNNNN)FFNNNNNNNNNNNNNNNNNTNNNNNNNF)NNNNNNNNNNNNNNN)NNFNN)FN)F)T)NNNr�)^rrr�__doc__r�r�rQ�FLG_DONT_CREATE_DBr$r1r*r7r;r>rFrHrCrcrdr�r�r�r�r�r�rrr)r+r�r=rArD�property�
domain_sidrHrJrGrOrUrXrZr\r^r`rcrerlrorqrsrtrr�r�r�r�r�r�r�r�r�r�r�r�r�rzr�rsr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r��
__classcell__rrr+rr4s�	


�-
�46
�Z
� 
�,
�f
�=
�
,+��
�


	

�7				




�^rc@sjeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�dsdb_Dnza class for binary DNNcCs�|dur|�d�r
tj}n|�d�rtj}ntj}|tjtjfvr]|�d�}t|�dkr2td|��dt|d�t|d�}|d|�|_	|j	d	t|d�d
�|_
||d�|_n	||_d|_	d|_
t�
||j�|_dS)zcreate a dsdb_DnNzB:zS:�:�z
Invalid DN %srOr�r�r�)�
startswithr�DSDB_SYNTAX_BINARY_DN�DSDB_SYNTAX_STRING_DN�DSDB_SYNTAX_OR_NAMErwrS�RuntimeErrorrV�prefix�binary�dnstringrQr�rU)r'�samdbr*�
syntax_oid�colons�
prefix_lenrrrr$�s$


zdsdb_Dn.__init__cCs|jt|jjdd��S)NrO)�mode)r(r9rUr�r4rrr�__str__szdsdb_Dn.__str__cCsH|}|}|j�d�}|j�d�}t||�}|dkr|St|j|j�}|S)z6 compare dsdb_Dn values similar to parsed_dn_compare()r�r)rU�get_extended_componentr
r))r'�other�dn1�dn2�guid1�guid2�vrrr�__cmp__s
zdsdb_Dn.__cmp__cCs|�|�dkS�Nr�r8�r'r2rrr�__eq__$r�zdsdb_Dn.__eq__cCs|�|�dkSr9r:r;rrr�__ne__'r�zdsdb_Dn.__ne__cCs|�|�dkSr9r:r;rrr�__lt__*r�zdsdb_Dn.__lt__cCs|�|�dkSr9r:r;rrr�__le__-r�zdsdb_Dn.__le__cCs|�|�dkSr9r:r;rrr�__gt__0r�zdsdb_Dn.__gt__cCs|�|�dkSr9r:r;rrr�__ge__3r�zdsdb_Dn.__ge__cCs|jdkrdSt|jd�S)z6return binary part of a dsdb_Dn as an integer, or Noner�N�)r(rVr)r4rrr�get_binary_integer6s
zdsdb_Dn.get_binary_integercCst�|j�S)zreturn binary as a byte string)�binascii�	unhexlifyr)r4rrrr	<r<zdsdb_Dn.get_bytesr�)rrrrr$r0r8r<r=r>r?r@rArCr	rrrrr�s
r)rrArQr9r2r-rrr�	samba.ndrrr�samba.dcerpcrr�samba.commonrr	r
rrrD�
__docformat__rrTrr�Ldbr�objectrrrrr�<module>s@N