a
    )&iu                     @   s   d dl Z d dlZd dlZd dlmZ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 e jd d d	ejd
< dZdZG dd deZedkrdZdZd dlZe  dS )    N)LdbErrorERR_OPERATIONS_ERROR
SCOPE_BASESCOPE_SUBTREE)security)
ndr_unpack)SamDB)credentials)KDCBaseTestz
bin/python1ZPYTHONUNBUFFEREDFc                   @   s:   e Zd ZdZdd Zdd Zdd Zdd
dZdd ZdS )	LdapTestszgTest for LDAP authentication using Kerberos credentials stored in a
       credentials cache file.
    c                 C   s   |    d S )N_run_ldap_testself r   </usr/lib/python3/dist-packages/samba/tests/krb5/test_ldap.py	test_ldap-   s    zLdapTests.test_ldapc                 C   s   | j dd d S )NT)renamer   r   r   r   r   test_ldap_rename0   s    zLdapTests.test_ldap_renamec                 C   s   | j dddd d S )NFT)include_pacexpect_anonallow_errorr   r   r   r   r   test_ldap_no_pac3   s    zLdapTests.test_ldap_no_pacFTc              
   C   s  |   }| }d}| j| jjdd}| }	|  }
| j||
|||d\}}| t	j
|j |jtd|	 dgd}| dt| ttj|d	 d d	 }|r|  }t| }t|tjd
|d
< || ztd| ||  d}W nX tyF } z>|s|   |j\}}| t| |  d| W Y d }~d S d }~0 0 |jdt!dgd}| dt| |d	 d }ttj|d	 }|r| tj"t#| ttj|d }| tj$t#| t|dkrttj|d }| tj%t#| n| || d S )NZldapF)Zaccount_typeZ	use_cache)Zpacz(sAMAccountName=%s)Z	objectSid)scopeZ
expressionattrs   r   ZsAMAccountName	ldap://%sZurlr	   ZlpZ NT_STATUS_NO_IMPERSONATION_TOKEN tokenGroupsr   r         )&	get_samdbhost_dns_nameZget_cached_credsZAccountTypeZUSERZget_usernameZget_dc_credsZcreate_ccache_with_userZ
addCleanuposremovenamesearchr   assertEquallenr   r   dom_sidZget_new_usernameldbZMessageZget_dnZMessageElementZFLAG_MOD_REPLACEZmodifyr   get_lpr   Zfailargsr   ZassertInr   SID_NT_ANONYMOUSstrZSID_NT_NETWORKZSID_NT_THIS_ORGANISATION)r   r   r   r   r   samdb	mach_nameZserviceZuser_credentialsZ	user_nameZmach_credentialsZcredsZ	cachefileldb_resZsidnew_namemsgldb_as_usereenumZestrZtoken_groups	token_sidr   r   r   r   7   sv    



zLdapTests._run_ldap_testc                 C   s   |   }| }t }|  td| ||  d}|jdtdgd}| 	dt
| ttj|d d d }| 	tjt| | 	t
|d d d d S )Nr   r   r   r    r!   r   r   )r$   r%   r	   ZCredentialsZset_anonymousr   r.   r)   r   r*   r+   r   r   r,   r0   r1   )r   r2   r3   Z
anon_credsr7   r4   r:   r   r   r   test_ldap_anonymous   s     zLdapTests.test_ldap_anonymousN)FTFF)	__name__
__module____qualname____doc__r   r   r   r   r;   r   r   r   r   r   (   s     
Wr   __main__)sysr&   r-   r   r   r   r   Zsamba.dcerpcr   Z	samba.ndrr   Zsamba.samdbr   Zsambar	   Zsamba.tests.krb5.kdc_base_testr
   pathinsertenvironZglobal_asn1_printZglobal_hexdumpr   r<   Zunittestmainr   r   r   r   <module>   s$   
|